httprequest,httpresponse,发送浏览流并在C#中接收结果[英] httpRequest, httpResponse, send GET through Stream and Receive the Result in C#

本文是小编为大家收集整理的关于httprequest,httpresponse,发送浏览流并在C#中接收结果的处理方法,想解了httprequest,httpresponse,发送浏览流并在C#中接收结果的问题怎么解决?httprequest,httpresponse,发送浏览流并在C#中接收结果问题的解决办法?httprequest,httpresponse,发送浏览流并在C#中接收结果问题的解决方案?那么可以参考本文帮助大家快速定位并解决问题,译文如有不准确的地方,大家可以切到English参考源文内容。

问题描述

这就是我要做的:

  • 连接到HTTP服务

  • 从这里开始,我需要与该流相处.

  • 现在,我发送了获取请求,服务回答我.

  • 然后,在第一次获得请求和答案之后,我需要每次服务时都会拦截一些东西.

我该怎么办?

我昨天从Httrequest,httresponse,getResponsestream等尝试,但不起作用:(

如何让流来与服务发送get请求的服务?

netcf 3.5.

非常感谢!

推荐答案

这是如何同步

的示例
WebRequest request = WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "text/html";

Stream reader = request.GetResponse().GetResponseStream();

和这里的异步样品

///........
WebRequest request = WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "text/html";

IAsyncResult result = request.BeginGetResponse(RequestCallback, request);
///........

private void RequestCallback(IAsyncResult ar)
{
     var request = ar.AsyncState as WebRequest;
     Stream reader = request.EndGetResponse(ar).GetResponseStream();
     //use this reader to read the content
}

本文地址:https://www.itbaoku.cn/post/2125019.html

问题描述

This is what i'm trying to do:

  • Connect to a http service

  • From here, i need to get a STREAM for comunicate with that.

  • Now, i send GET request, and the service answer me.

  • Then, after the first GET request and the answer, i need to intercept everytime the service send me something.

How can i do?

I'm trying from yesterday with httRequest, httResponse, GetResponseStream and so on, but not working :(

How can i have the stream to "talk" with the service sending the GET request?

all this for NETCF 3.5.

Thanks a lot!

推荐答案

Here is a sample of how to do it synchronously

WebRequest request = WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "text/html";

Stream reader = request.GetResponse().GetResponseStream();

and here an asynchronous sample

///........
WebRequest request = WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "text/html";

IAsyncResult result = request.BeginGetResponse(RequestCallback, request);
///........

private void RequestCallback(IAsyncResult ar)
{
     var request = ar.AsyncState as WebRequest;
     Stream reader = request.EndGetResponse(ar).GetResponseStream();
     //use this reader to read the content
}
查看更多