I am running a following code on Windows Phone:
string baseAddress = tcAddress + "/Api/Audio/RegisterAudioThoughtUpload/";
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(baseAddress);
request.Method = "POST";
request.ContentType = "application/json; charset=utf-8";
int total = 1633;
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
string json = "{ \"AnonymousUserId\":\"" + Guid.NewGuid().ToString() + "\", \"TotalSize\":\"" + total.ToString() + "\" }";
streamWriter.Write(json);
streamWriter.Close();
}
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
int statusCode = (int)response.StatusCode; \\At this point response is null
StreamReader reader = new StreamReader(response.GetResponseStream());
string results = reader.ReadToEnd();
response.Close();
}
Basically I am trying to send a json-packed guid and an int to an mvc3 action.
On the server side, the processing action is:
[HttpPost]
public JsonResult RegisterAudioThoughtUpload(string AnonymousUserId, int TotalSize)
{
var aUId = Guid.Parse(AnonymousUserId);
return new JsonResult {Data = Guid.NewGuid()};
}
A fiddler2, that is logging the communication shows, that the server is providing a client an expected response:

But on the client, a call to GetResponse() just times out, never actually getting a response back.
Since it is Windows Phone, a GetResponse() is implemented the following way:
private const int DefaultRequestTimeout = 15000;
public static HttpWebResponse GetResponse(this HttpWebRequest request)
{
var dataReady = new AutoResetEvent(false);
HttpWebResponse response = null;
var callback = new AsyncCallback(delegate(IAsyncResult asynchronousResult)
{
response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
dataReady.Set();
});
request.BeginGetResponse(callback, request);
if (dataReady.WaitOne(DefaultRequestTimeout))
{
return response;
}
return null;
}
Why I am not getting an instance of HttpWebResponse, even though I see through proxy, that it is being sent?
Since this is a WP application I would recommend you avoid blocking the main UI thread. I’d also recommend you using a JSON serializer instead of manually build JSON strings. So let’s start by defining our models:
and then:
Also another problem I can see in the fiddler Response you have shown from the server is that the
Content-Typeheader was set toapplication/jsonand yet the contents is not valid JSON:instead of:
As far as the timeout error is concerned, maybe the response doesn’t get to the phone. Check your network.