做httpwebrequest但不等待回应

时间:2013-04-01 16:48:48

标签: c# asp.net httpwebrequest webrequest httpwebresponse

在这里,我不想等待服务器的响应,因为我不关心响应 这些httpwebrequest。

没关系?或者它将来会影响系统吗?

HttpWebRequest PostRequest = (HttpWebRequest)WebRequest.Create(PostUrl);
        PostRequest.ContentType = PostContentType;
        PostRequest.Method = "POST";
        byte[] bytes = Encoding.ASCII.GetBytes(DataUrl);            
        try
        {
            PostRequest.ContentLength = bytes.Length;
            using (Stream webpageStream = PostRequest.GetRequestStream())
            {
                webpageStream.Write(bytes, 0, bytes.Length);
            }
        }
        catch (Exception ex)
        {
            //
        }

由于

3 个答案:

答案 0 :(得分:2)

您可以调用异步BeginGetRequestStream(),然后在完成时处理响应数据。如果您不需要响应数据,那么您可以忽略它,但您可能有兴趣获取响应状态代码以检查您的请求是否成功。

答案 1 :(得分:2)

是的,别忘了添加这些行

  webpageStream.Flush();
  webpageStream.Close();

答案 2 :(得分:0)

是的,这个答案是如此真实,删除此代码:

onDeviceReady: function() {
        app.receivedEvent('deviceready');

          BMSClient.initialize("xxxxx","xxxxx");

        MFPPush.registerDevice({}, function(data){

            alert("Success ::" + data)
        }, function(error){
            alert("Failure ::" + error);
        }); 

        var notificationsCallback = function(notification){
            alert("Incoming notification :: " + JSON.stringify(notification))           
        };

        MFPPush.registerNotificationsCallback(notificationsCallback);

    }

并在stream.write:

之后使用这样的流添加流动的代码
var response = (HttpWebResponse)request.GetResponse();

var responseString = new SreamReader(response.GetResponseStream()).ReadToEnd();