我快疯了,关于wp http post 的问题
不管我做任何的处理或者不处理, 当调用
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), data);
线程就阻塞了,永远也没用办法触发 GetResponseCallback,即使我随便填了一个url,一般来说就算http请求时错误的,那也总得有个结果吧,代码;
Http?Post
private ResultData HttpPost(MemoryStream stream)
{
try
{
stream.Position = 0;
trace = Trace(stream);
HttpWebRequest request = WebRequest.CreateHttp(ConfigurationManager.BaseUrl);
request.ContentType = "application/json";
request.Method = "POST";
request.ContentLength = stream.Length;
AsyncRequestData data = new AsyncRequestData()
{
Request = request,
Data = stream
};
request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), data);
_PostDoned.WaitOne();
return Result;
}
catch (Exception ex)
{
throw new Exception("请求出错");
}
}
private void GetRequestStreamCallback(IAsyncResult asyncResult)
{
try
{
AsyncRequestData asyncData = asyncResult.AsyncState as AsyncRequestData;
HttpWebRequest request = asyncData.Request;
MemoryStream mStream = asyncData.Data as MemoryStream;
Stream httpStream = request.EndGetRequestStream(asyncResult);
httpStream.Write(mStream.ToArray(), 0, (int)mStream.Length);
httpStream.Close();
AsyncRequestData data = new AsyncRequestData()
{
Request = request,
Data = httpStream
};
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), data);
Thread.Sleep(100);
}
catch
{
throw new Exception("服务器无响应");
}
}
private void GetResponseCallback(IAsyncResult asyncResult)
{
Stream httpStream = null;
try
{
AsyncRequestData asyncData = asyncResult as AsyncRequestData;
HttpWebRequest request = asyncData.Request;
httpStream = asyncData.Data as Stream;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);
httpStream = response.GetResponseStream();
DataContractJsonSerializer rspDataSerializer = new DataContractJsonSerializer(typeof(ResultData));
Result = (ResultData)rspDataSerializer.ReadObject(httpStream);
_PostDoned.Reset();
}
catch
{
throw new Exception("响应出错");
}
finally
{
if (httpStream != null)
httpStream.Close();
}
}
public void PostResult(string url, string postParas)
{
HttpWebRequest request = HttpWebRequest.CreateHttp(url);
request.Method = "POST";
request.AllowReadStreamBuffering = true;
try
{
request.BeginGetRequestStream((asyncResult) =>
{
byte[] header = System.Text.Encoding.UTF8.GetBytes(postParas);
HttpWebRequest postRequest = asyncResult.AsyncState as HttpWebRequest;
Stream streamResult = postRequest.EndGetRequestStream(asyncResult);
//HttpWebResponse webResponse = (HttpWebResponse)webRequest.EndGetResponse(asyncResult);
//得到响应流
streamResult.Write(header, 0, header.Length);
streamResult.Flush();
streamResult.Close();
postRequest.BeginGetResponse((asyncPostResult) =>
{
HttpWebRequest webRequest = (HttpWebRequest)asyncResult.AsyncState;
HttpWebResponse webResponse = (HttpWebResponse)webRequest.EndGetResponse(asyncResult);
if (OnGetCompleted != null)
{
OnGetCompleted(this, webResponse.StatusCode.ToString());
}
}, postRequest);
}, request);
}
catch (WebException ex)
{
MessageBox.Show(ex.Message);
}
}