httpRequest, httpResponse, send GET through Stream and Receive the Result in C#

拈花ヽ惹草 提交于 2019-12-25 04:19:16

问题


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!


回答1:


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
}


来源:https://stackoverflow.com/questions/4633132/httprequest-httpresponse-send-get-through-stream-and-receive-the-result-in-c-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!