Reputation: 1901
Sorry, it's questions already was on stackoverflow, but I don't found answer. Maybe it's classic, but I can't understand, how to solve this problem :( I have Singletone class for working with web service:
public sealed class GYAccessService
{
static GYAccessService instance = null;
static readonly object padlock = new object();
public string Atoken { get; set; }
public string Login { get; set; }
public string Password { get; set; }
public string Uid { get; set; }
const string UrlRequest = "http://site.html";
private GYAccessService()
{
}
public static GYAccessService Instance
{
get
{
lock (padlock)
{
if (instance == null)
{
instance = new GYAccessService();
}
return instance;
}
}
}
public void sendPost(string postData)
{
PostData = "request=" + postData;
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(UrlRequest);
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Accept = "application/json";
webRequest.AllowAutoRedirect = true;
// Start the request
webRequest.BeginGetRequestStream(new AsyncCallback(getRequestStreamCallback), webRequest);
}
private void getRequestStreamCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
// End the stream request operation
Stream postStream = webRequest.EndGetRequestStream(asynchronousResult);
byte[] byteArray = Encoding.UTF8.GetBytes(PostData);
// Add the post data to the web request
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();
// Start the web request
webRequest.BeginGetResponse(new AsyncCallback(getResponseCallback), webRequest);
}
private void getResponseCallback(IAsyncResult asynchronousResult)
{
try
{
HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response;
// End the get response operation
response = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult);
Stream streamResponse = response.GetResponseStream();
StreamReader streamReader = new StreamReader(streamResponse);
var Response = streamReader.ReadToEnd();
streamResponse.Close();
streamReader.Close();
response.Close();
using (MemoryStream stream = new MemoryStream(Encoding.Unicode.GetBytes(Response)))
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(ServiceResponse));
ServiceResponse res = (ServiceResponse)serializer.ReadObject(stream);
// I want to return res in main thread
}
}
catch (WebException e)
{
// Error treatment
ServiceResponse err = new ServiceResponse();
err.Message = e.Message;
}
}
public string getRequestString(string model, string method, Params parametrs, string auth = null)
{
Function func = new Function();
func.Model = model;
func.Method = method;
func.Params = parametrs;
ServiceRequest req = new ServiceRequest() { Function = func };
string json = JsonConvert.SerializeObject(req);
ServiceResponse deserializedProduct = JsonConvert.DeserializeObject<ServiceResponse>(json);
return json;
}
}
I have call sendPost() in main thread, I want to return response in this function.
GYAccessService gy = GYAccessService.Instance;
gy.sendPost(postData);
//I want to use the response here
But in WP 7 HttpWebRequest has not GetResponse, only BeginGetResponse. I use callback and don't know how can I return response from callback. I put up with it, I can set property response my Singletone and get it after end async callback, but I don't know how can I block async callback. It's really problem, I try to use WaitOne()/Set() - don't help me, my app just frozen. I try to use IAsyncResult handle, but not success. How can I wait end to work async callback? Thanks in advance!
Upvotes: 1
Views: 792
Reputation: 16361
You need to use the EndGetResponse
method and process the result in it. It is asynchronous, so you will not be able to immediately get the result after you call gy.sendPost(postData);
, you have to use it as a call back.
If I may suggest, there is a simpler way for such HTTP calls, use RestSharp. It is a great and easy to use library that also works with Windows Phone.
Upvotes: 1