If you have unrestricted internet access then the "WifiWebClient" example will work right out of the box. But that is not the case with me, i am behind a proxy server. Hence in this post i will tell you how i worked around this problem in details.
If you upload the code then you will see that it fails to connect. Now if you read How Proxy Servers work then you will realize that instead of connecting directly to the required server/page you actually need to connect to the proxy server and request it to fetch the required data from the webpage for you.
Hence in the “server” variable we will write the IP of our proxy server which in my case is 202.141.80.24. Now the port information is also very important. The default port used in the code is 80 but you might need to change it according to your needs, in my case it is 3128 as specified to us by the university. Now its time to upload the code and check what happens.
#ifndef __CC3200R1M1RGC__#include <SPI.h>#endif#include <WiFi.h>// your network name also called SSIDchar ssid[] = "******";// your network passwordchar password[] = "*****;// if you don't want to use DNS (and reduce your sketch size)// use the numeric IP instead of the name for the server:IPAddress server(202,141,80,24);  // numeric IP for Google (no DNS)WiFiClient client;void setup() {//Initialize serial and wait for port to open:Serial.begin(115200);// attempt to connect to Wifi network:Serial.print("Attempting to connect to Network named: ");// print the network name (SSID);Serial.println(ssid);// Connect to WPA/WPA2 network. Change this line if using open or WEP network:WiFi.begin(ssid, password);while ( WiFi.status() != WL_CONNECTED) {// print dots while we wait to connectSerial.print(".");delay(300);}Serial.println("\nYou're connected to the network");Serial.println("Waiting for an ip address");while (WiFi.localIP() == INADDR_NONE) {// print dots while we wait for an ip addresssSerial.print(".");delay(300);}Serial.println("\nIP Address obtained");printWifiStatus();Serial.println("\nStarting connection to server...");// if you get a connection, report back via serial:if (client.connect(server, 3128)) {Serial.println("connected to server");// Make a HTTP request:client.println("GET /hello.html HTTP/1.1");client.println("Host: energia.nu");client.println("Connection: close");client.println();}}void loop() {// if there are incoming bytes available// from the server, read them and print them:while (client.available()) {char c = client.read();Serial.write(c);}// if the server's disconnected, stop the client:if (!client.connected()) {Serial.println();Serial.println("disconnecting from server.");client.stop();// do nothing forevermore:while (true);}}void printWifiStatus() {// print the SSID of the network you're attached to:Serial.print("SSID: ");Serial.println(WiFi.SSID());// print your WiFi shield's IP address:IPAddress ip = WiFi.localIP();Serial.print("IP Address: ");Serial.println(ip);// print the received signal strength:long rssi = WiFi.RSSI();Serial.print("signal strength (RSSI):");Serial.print(rssi);Serial.println(" dBm");}
On the serial monitor this is what i saw -> we get a 400:bad request error.
After some researching on that issue i came across this wonderful page. According to this page, when requesting through a proxy the GET request must be absolute.
The absoluteURI form is required when the request is being made to a proxy. Proxy server will convert absolute URI to relative URI.
Note: We are connecting to energia.nu/hello.html in this example which just displays “Hello world!”.
Hence we need to enter the absolute URI of the page, for that we change that code to —
client.println("GET http://energia.nu/hello.html HTTP/1.1");
After uploading i got this -> The authentication 407 error.
We are making some progress aren’t we !!.
Now we need to figure out how to send the authentication data in the http request as well.
So upon reading the HTTP1.1 manual i saw that the proxy authentication data goes as another header which is like this -> Proxy-Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
Here is a list of all the headers. Scroll down to approximately 1/3rd of the page to see this –
Now the important thing to understand is that your details go in this format : Proxy-Authorization: Basic username:password. But the “username:password” string needs to be base64 encoded. You can use any online utility to do that. Maybe something like this .
So i made these changes to the request –
client.println("GET http://energia.nu/hello.html HTTP/1.1");client.println("Host: energia.nu");client.println("Proxy-Authorization: Basic your_base64_encoded_details_go_here");client.println("Connection: close");client.println();
And voila it worked!! Here is a screen shot and as you can see the message -> 200: Ok and the “Hello World!” message that the webpage has !.
This is how i got my Launchpad to work across proxy !!. Enjoy, till then keep making and hacking!!
Comments
Please log in or sign up to comment.