httpRequest,httpResponse,通过Stream发送GET并在C#中接收结果 [英] httpRequest, httpResponse, send GET through Stream and Receive the Result in C#

查看:394
本文介绍了httpRequest,httpResponse,通过Stream发送GET并在C#中接收结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我想要做的:

  • 连接到http服务

  • Connect to a http service

从这里开始,我需要获取一个STREAM进行通信.

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

现在,我发送GET请求,服务会回答我.

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

然后,在第一个GET请求和答案之后,每次服务向我发送东西时,我都需要拦截.

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

我该怎么办?

我从昨天开始尝试使用httRequest,httResponse,GetResponseStream等,但是不起作用:(

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

我如何让流与发送GET请求的服务交谈"?

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

对于NETCF 3.5,所有这些.

all this for NETCF 3.5.

非常感谢!

推荐答案

以下是如何同步进行操作的示例

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();

这里是一个异步示例

///........
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
}

这篇关于httpRequest,httpResponse,通过Stream发送GET并在C#中接收结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆