Connecting CC3200 to internet via Proxy Server with authentication

 

DSC02320If you have unrestricted internet access then this 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.
The example we will be using is “WifiWebClient” example.
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.

Diagram of two computers connected only via a proxy server. The first computer says to the proxy server: "ask the second computer what the time is".

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 SSID
char ssid[] = "******";
// your network password
char 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 connect
Serial.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 addresss
Serial.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.

400: Bad RequestAfter 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.

direct_vs_proxy
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.

407:Proxy AuthenticationWe 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 –

Proxy headerNow 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 !.

success

This is how i got my Launchpad to work across proxy !!. Enjoy, till then keep making and hacking !!.

Advertisements

About Shantam Raj

Currently I am a final year B.Tech undergraduate majoring in “Electronics and Communication Engineering” from IIT Guwahati. I am passionate about electronics and robotics. Apart from that I love writing, visit https://medium.com/@shantam_raj for some of my works. I am a die hard soccer fan. I also love to play badminton. I am a hardware hacking enthusiast and a tinkerer !!.
This entry was posted in CC3200, Internet of Things, Proxy Server and tagged , , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s