HttpWebRequest BeginGetResponse无效

时间:2015-10-19 19:15:14

标签: c# asynchronous httpwebrequest httprequest httpwebresponse

我尝试使用HttpWebRequest,我的BeginGetRequestStream可以工作,但它从未进入BeginGetResponse函数,我不知道为什么..我已经搜索了几个小时但没有找到有效的解决方案

    public void Initialize(IScheduler scheduler)
    {
        if(_isCloud)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_cloudMappingServer + "/Mapping/GetAllCentralPoints");

            request.ContentType = "application/x-www-form-urlencoded";
            request.Method = "POST";
            request.BeginGetRequestStream(new AsyncCallback(ReleaseReadCallback), request);

            // Instruct the thread to wait until we resume it
            _waitHandle.WaitOne();
            _waitHandle.Dispose();
        }
    }

    private void ReleaseReadCallback(IAsyncResult asynchronousResult)
    {
        try
        {
            HttpWebRequest httpRequest = (HttpWebRequest)asynchronousResult.AsyncState;

            using (Stream postStream = httpRequest.EndGetRequestStream(asynchronousResult))
            {
                using (MemoryStream memStream = new MemoryStream())
                {
                    string queryString = string.Empty;
                    byte[] bytes = System.Text.Encoding.UTF8.GetBytes(queryString);
                    memStream.Write(bytes, 0, bytes.Length);
                    memStream.Position = 0;

                    byte[] tempBuffer = new byte[memStream.Length];
                    memStream.Read(tempBuffer, 0, tempBuffer.Length);
                    postStream.Write(tempBuffer, 0, tempBuffer.Length);
                }
            }

            httpRequest.BeginGetResponse(new AsyncCallback(ReleaseResponseCallback), httpRequest);
        }
        catch (Exception ex)
        {
            var test = ex;
        }
    }

    private void ReleaseResponseCallback(IAsyncResult asynchronousResult)
    {
        HttpWebRequest responseRequest = (HttpWebRequest)asynchronousResult.AsyncState;
        string responseString = string.Empty;

        try
        {
            using (HttpWebResponse resp = (HttpWebResponse)responseRequest.EndGetResponse(asynchronousResult))
            {
                using (StreamReader streamRead = new StreamReader(resp.GetResponseStream()))
                {
                    responseString = streamRead.ReadToEnd();

                    try
                    {
                        JsonSerializerSettings settings = new JsonSerializerSettings();
                        List<CentralPointViewModel> _allCentralPointViewModel = JsonConvert.DeserializeObject<List<CentralPointViewModel>>(responseString, settings);
                    }
                    catch (JsonReaderException)
                    {
                        responseString = responseString.Replace('\"'.ToString(), string.Empty);
                        string[] responseArray = responseString.Split(';');
                    }
                    catch (JsonSerializationException)
                    {
                        responseString = responseString.Replace('\"'.ToString(), string.Empty);
                    }
                }
            }
        }
        catch (Exception ex)
        {
        }
    }

它永远不会进入ReleaseResponseCallback功能!我能够拨打我的服务器电话,但响应从未到达我或我没有正确接收它..任何帮助表示赞赏

0 个答案:

没有答案