问题描述
在我的Xamarin应用程序中,我使用HttpWebRequest类将POST消息发送到服务器(之所以使用它,是因为它在PCL库中是现成的).
In my Xamarin application I use HttpWebRequest class to send POST messages to the server (I use it because it is available out-of-the box in PCL libraries).
以下是一些请求准备代码:
Here is some request preparation code:
request.BeginGetRequestStream (asyncResult => {
Mvx.Trace ("BeginGetRequestStream callback");
request = (HttpWebRequest)asyncResult.AsyncState;
Stream postStream = request.EndGetRequestStream (asyncResult);
string postData = jsonConverter.SerializeObject (objectToSend);
Mvx.Trace ("Posting following JSON: {0}", postData);
byte[] byteArray = Encoding.UTF8.GetBytes (postData);
postStream.Write (byteArray, 0, byteArray.Length);
MakeRequest (request, timeoutMilliseconds, successAction, errorAction);
}, request);
当我启动应用程序并第一次和第二次执行此代码时,一切正常.但是,当它第三次执行时(恰好是!),则不会调用该回调,并且永远不会将"BeginGetRequestStream回调"行打印到日志中.这是类实现中的错误,还是我做错了什么?
When I start application and execute this code for the first and the second time everything works fine. But when this is executed for the 3rd time (exactly!) the callback is not called and line "BeginGetRequestStream callback" is never printed to log. Is it a bug in class implementation or maybe I do something incorrectly?
如果无法在Xamarin中使其正常工作,请建议使用可靠且方便的类来发送带有超时的Http GET和POST请求.
If it is not possible to make this working in Xamarin please suggest reliable and convenient class for sending Http GET and POST request with timeout.
还创建了相关的更普遍的问题:从Xamarin便携式类库发送Http请求
Also created related, more general question: Sending Http requests from Xamarin Portable Class Library
推荐答案
最后通过切换到Profile 78和HttpClient来解决此问题,它们在所有情况下均能正常工作.
Finally solved this by switching to Profile 78 and HttpClient, which works well in all cases.
这篇关于从未调用HttpWebRequest BeginGetRequestStream回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!