1

对不起,问题已经在stackoverflow上,但我没有找到答案。也许它是经典的,但我不明白,如何解决这个问题:(我有使用 Web 服务的 Singletone 类:

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;
    }
}

我在主线程中调用了 sendPost(),我想在这个函数中返回响应。

 GYAccessService gy = GYAccessService.Instance;
 gy.sendPost(postData); 
 //I want to use the response here

但是在 WP 7 HttpWebRequest 中没有 GetResponse,只有 BeginGetResponse。我使用回调,但不知道如何从回调返回响应。我忍受了,我可以设置属性响应我的 Singletone 并在结束异步回调后获取它,但我不知道如何阻止异步回调。这真的是个问题,我尝试使用 WaitOne()/Set() - 别帮我,我的应用程序刚刚冻结。我尝试使用 IAsyncResult 句柄,但没有成功。如何等待结束工作异步回调?提前致谢!

4

1 回答 1

1

您需要使用该EndGetResponse方法并在其中处理结果。它是异步的,所以你调用后无法立即得到结果gy.sendPost(postData);,你必须将其用作回调。

如果我可以建议,这种 HTTP 调用有一种更简单的方法,请使用RestSharp。它是一个很棒且易于使用的库,也适用于 Windows Phone。

于 2012-12-29T12:37:05.207 回答