I would like to return the object after the post HTTP Post or Get request. I could do on web application. Now I need to write this function the MS windows phone application. I read some article about performing an HTTP Get and Post Request, but I still cannot figure it out. I would like have a helper class to do the web request and return the object as same as I did on my web application. My windows application uses MVVM pattern. The web request will be called by viewmodel.How can I overload the BeginGetResponse to return object rather than IAsyncResult? Would someone give me the link or example to guide me. Thanks in advance.
There is my old code on web application
public static T GetData<T>(Uri relativeUri)
{
var request = CreateRequest(relativeUri);
HttpWebResponse r;
try
{
r = (HttpWebResponse)request.GetResponse();
return Deserializer<T>(r.GetResponseStream());
}
catch (WebException webex)
{
HttpWebResponse webResp = (HttpWebResponse)webex.Response;
setSessionError(webResp.GetResponseStream());
}
return Deserializer<T>(request.GetResponse().GetResponseStream());
}
public static T Deserializer<T>(Stream s)
{
//Get results
var ser = new DataContractSerializer(typeof(T));
var reader = XmlDictionaryReader.CreateTextReader(s,
new System.Xml.XmlDictionaryReaderQuotas());
ser = new DataContractSerializer(typeof(T));
var deserializedItem = (T)ser.ReadObject(reader, true);
reader.Close();
return deserializedItem;
}
I want to do something like that:
public static T GetData<T>(Uri relativeUri)
{
var request = CreateRequest(relativeUri);
Stream ResponseStream;
request.BeginGetResponse(ReadCallback, request, ResponseStream);
return Deserializer<T>(ResponseStream);
}