I'm trying to build and run old application which worked fine several month ago on wp7, however none of http clients works for me.
First, i'm trying HttpWebRequest
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.Method = HttpMethod.Get;
httpWebRequest.Accept = "application/json";
var response = (HttpWebResponse)await httpWebRequest.GetResponseAsync();
it throws an exception
The remote server returned an error: NotFound.
Then i tried HttpClient
Uri theUri = new Uri("https://www.google.fi/");
HttpClient aClient = new HttpClient();
aClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
aClient.DefaultRequestHeaders.Host = theUri.Host;
//Post the data
HttpResponseMessage aResponse = await aClient.PostAsync(theUri, new StringContent(postData));
if (aResponse.IsSuccessStatusCode)
{
return aResponse.Content.ToString();
}
else
{
// show the response status code
return "HTTP Status: " + aResponse.StatusCode.ToString() + " - Reason: " + aResponse.ReasonPhrase;
}
it returns
System.Net.HttpStatusCode.NotFound
Looking like phone cant reach network at all... Though:
- My current approach (via HttpWebRequest) definitely worked before
- I definitely have network access, because other network apps works more or less fine.
- BONUS: the same app on wp8 works fine. So server is perfectly available and request is valid.
EDIT1: found here a similar issue, added
client.Headers["Content-Type"] = "application/x-www-form-urlencoded";
client.Encoding = Encoding.UTF8;
but still getting same error.
EDIT2: made a fresh wp7 project, added Bcl.Async and HttpClient. Still the same problem.
EDIT3: last night research:
on wp7 device this works ok:
var client = new RestClient("http://example.com");
var request = new RestRequest(String.Empty, Method.GET);
client.GetAsync(request, (_response, _handle) =>
{
var resource = _response;
var content = resource.Content;
});
But when i'm switching to my server, it throws NotFound exception on wp7 device. On wp8 device it returns correct result.
Resetting to factory settings helped.