Hi,
I am working on a metro app in which I am sending a web request in background service but not getting web response. I coded like this:
private void SendRequest(string url, Action<string> OnResponse, string requestParameter)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url));
request.Method = "POST";
request.ContentType = "text/xml";
request.BeginGetRequestStream(delegate(IAsyncResult req)
{
var outStream = request.EndGetRequestStream(req);
using (StreamWriter w = new StreamWriter(outStream))
w.Write(requestParameter);
request.BeginGetResponse(delegate(IAsyncResult result)
{
try
{
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result);
using (var stream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream))
{
string response1 = reader.ReadToEnd();
OnResponse(response1);
}
}
}
catch (Exception ex)
{
}
}, null);
}, null);
}
I also tried like this
HttpClient client = new HttpClient();
HttpContent content = new StringContent(requestParameter);
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("text/xml");
HttpResponseMessage response = await client.PostAsync(url, content);
Is there any reason why I am not getting response or any other method to do this. I executed the same code in normal execution and works perfect but not in background service.