Hello guys!
I’m trying to access a URL using HttpWebRequest class but, somehow the device seems not connecting to internet.
This is the code to setup WiFi(tried DHCP and even Static IP):
private void SetupWifi()
{
wifi.NetworkUp += new GTM.Module.NetworkModule.NetworkEventHandler(wifi_NetworkUp);
var network = wifi.Search("automax");
//var network = wifi.Search("GUTO_WP7_WORK");
//wifi.UseDHCP();
//wifi.UseStaticIP("192.168.0.51", "255.255.255.0", "192.168.0.1", new string[] { "192.168.0.1" });
wifi.Join(network, "123@ @ rfine");
//wifi.Join(network, "120880guto");
}
This is the code I’m trying to get the HttpResponse:
public void BeginRestRequest(WebMethod method, string action, object requestBody)
{
var request = WebRequest.Create(_baseUrl + action) as HttpWebRequest;
HttpWebResponse response;
request.Method = method.ToString();
try
{
switch (method)
{
case WebMethod.GET:
{
response = request.GetResponse() as HttpWebResponse;
var reader = new StreamReader(response.GetResponseStream());
var jsonString = reader.ReadToEnd();
GETRequestDone(new RestEventArgs() { Result = JsonParser.JsonDecode(reader.ReadToEnd()) });
}
break;
case WebMethod.POST:
{
var jsonSerializer = new Serializer();
if (requestBody == null)
throw new ArgumentNullException("POST request can't have an empty body...");
var buffer = Encoding.UTF8.GetBytes(jsonSerializer.Serialize(requestBody));
request.ContentType = "application/json";
request.ContentLength = buffer.Length;
Stream postData = request.GetRequestStream();
postData.Write(buffer, 0, buffer.Length);
postData.Close();
response = request.GetResponse() as HttpWebResponse;
var reader = new StreamReader(response.GetResponseStream());
var jsonString = reader.ReadToEnd();
POSTRequestDone(new RestEventArgs() { Result = jsonSerializer.Deserialize(reader.ReadToEnd()) });
}
break;
case WebMethod.PUT:
{
var jsonSerializer = new Serializer();
var buffer = Encoding.UTF8.GetBytes(jsonSerializer.Serialize(requestBody));
request.ContentType = "application/json";
request.ContentLength = buffer.Length;
Stream postData = request.GetRequestStream();
postData.Write(buffer, 0, buffer.Length);
postData.Close();
response = request.GetResponse() as HttpWebResponse;
var reader = new StreamReader(response.GetResponseStream());
var jsonString = reader.ReadToEnd();
PUTRequestDone(new RestEventArgs() { Result = jsonSerializer.Deserialize(reader.ReadToEnd()) });
}
break;
case WebMethod.DELETE:
{
var jsonSerializer = new Serializer();
var buffer = Encoding.UTF8.GetBytes(jsonSerializer.Serialize(requestBody));
request.ContentType = "application/json";
request.ContentLength = buffer.Length;
Stream postData = request.GetRequestStream();
postData.Write(buffer, 0, buffer.Length);
postData.Close();
response = request.GetResponse() as HttpWebResponse;
var reader = new StreamReader(response.GetResponseStream());
var jsonString = reader.ReadToEnd();
DELETERequestDone(new RestEventArgs() { Result = jsonSerializer.Deserialize(reader.ReadToEnd()) });
}
break;
default:
break;
}
}
catch (Exception ex)
{
throw;
}
}
And this the exception that is throw when I try to read the response(access internet resource):
Program Started
#### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (1) ####
#### Message:
#### Microsoft.SPOT.Net.SocketNative::getaddrinfo [IP: 0000] ####
#### System.Net.Dns::GetHostEntry [IP: 0008] ####
#### System.Net.HttpWebRequest::EstablishConnection [IP: 00e1] ####
#### System.Net.HttpWebRequest::SubmitRequest [IP: 0013] ####
#### System.Net.HttpWebRequest::GetResponse [IP: 000c] ####
#### RFine.NETMF.Engine.Networking.RestClient::BeginRestRequest [IP: 0034] ####
#### GPSTest.Program::ProgramStarted [IP: 0027] ####
#### GPSTest.Program::Main [IP: 0015] ####
#### SocketException ErrorCode = -1
#### SocketException ErrorCode = -1
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in Microsoft.SPOT.Net.dll
#### SocketException ErrorCode = -1
#### SocketException ErrorCode = -1
#### Exception System.Net.WebException - 0x00000000 (1) ####
#### Message: host not available
#### System.Net.HttpWebRequest::EstablishConnection [IP: 00f1] ####
#### System.Net.HttpWebRequest::SubmitRequest [IP: 0013] ####
#### System.Net.HttpWebRequest::GetResponse [IP: 000c] ####
#### RFine.NETMF.Engine.Networking.RestClient::BeginRestRequest [IP: 0034] ####
#### GPSTest.Program::ProgramStarted [IP: 0027] ####
#### GPSTest.Program::Main [IP: 0015] ####
A first chance exception of type 'System.Net.WebException' occurred in System.Http.dll
#### Exception System.Net.WebException - 0x00000000 (1) ####
#### Message:
#### System.Net.HttpWebRequest::GetResponse [IP: 00d3] ####
#### RFine.NETMF.Engine.Networking.RestClient::BeginRestRequest [IP: 0034] ####
#### GPSTest.Program::ProgramStarted [IP: 0027] ####
#### GPSTest.Program::Main [IP: 0015] ####
A first chance exception of type 'System.Net.WebException' occurred in System.Http.dll
Even if I try a simple Dns.GetHostEntry(“www.ghieletronics.com”) will throw this exception.
What I’m doing wrong? The URL I’m trying to access is available and working on my Silverlight Application, Windows Phone 7 Application, Windows 8 Metro App. So, this is not a server related problem.
Thanks!
Best regards…