首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

服务端如何避免http请求

2012-01-08 
服务端如何处理http请求我在winform程序发送请求,服务端处理后返回数据,请问服务端程序如何实现处理这个。

服务端如何处理http请求
我在winform程序发送请求,服务端处理后返回数据,请问服务端程序如何实现处理这个。    
 
以下是winform发送请求程序    
PostData为发送到服务端的数据流    
 
Stream     reStream     =     null;     //返回    
cookie     =     null;    
HttpWebRequest     request     =     (HttpWebRequest)WebRequest.Create(url);            
                                                              request.ContentType     =     "application/x-www-form-urlencoded ";    
                                                              request.ContentType     =     "text/html;     charset=gb2312 ";    
                                                              request.KeepAlive     =     false;    
                                                              request.Method     =     "POST ";    
                                                              request.AllowAutoRedirect     =     false;    
                                                              request.CookieContainer     =     new     CookieContainer();    
                                                              byte[]     buffer=     new     byte[postData.Length];    
                                                              postData.Read(buffer,     0,     buffer.Length);    
                                                              request.ContentLength     =     buffer.Length;    
                                                              Stream     pstream     =     request.GetRequestStream();    
                                                              pstream   .Write(buffer,     0,     buffer.Length);    


                                                              pstream   .Close();    

HttpWebResponse     response     =     (HttpWebResponse)request.GetResponse();    
                                                              cookie     =     request.CookieContainer;    
                                                              reStream     =     CopyStream(response.GetResponseStream());    
                                                              return     reStream;

[解决办法]
晕 还不知道啊!!
流程是这样的:
1 客户端向服务器发请求
CString url = "www.163.com/id=100 ";
WebRequest myWebRequest = WebRequest.Create(url);
myWebRequest.Timeout=10000;

2 服务器端受到请求
用 CString strid = request[ "id "];接收参数 如果url没有参数就不用接收
得到参数你做什么处理就是你的事了 然后 把处理后的数据 Response.wirte 出来

3
客户端收数据
WebResponse myWebResponse = myWebRequest.GetResponse();
Stream ReceiveStream = myWebResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding(encoding);
StreamReader readStream = new StreamReader( ReceiveStream, encode );
readStream 就是从服务器端得到的数据 是图像是文字你自己操作吧


[解决办法]
为了100分,还是说说话
我猜你的要求是通过HTTP POST 方式发送XML字符串到服务器端
如果是这样
request.ContentType = "application/x-www-form-urlencoded ";
request.ContentType = "text/html; charset=gb2312 ";
改为
request.ContentType = "text/xml; ";

可以在ASP.net里建个空页面 在page_load里作处理
处理代码大概如下
using (StreamReader readStream = new StreamReader(Request.InputStream, Encoding.Default))
{
result = readStream.ReadToEnd();
}
Response.Write(result);
[解决办法]
Client使用WinForm,发送使用WebHttpRequest,接收使用WebHttpResponse,这点没有问题。

Server应该使用Web Server,设立一个aspx页面,内容为空,在Page_Load事件处理。

如果发送的是http://xxx.xxx.xxx?req=画图这样的格式,使用Request.QueryString来获取,比如Request.QueryString[ "req "].ToString(),这样就获得了 "画图 "。注意,请使用Request.QueryString.AllKeys.Count> 0来判断是否有参数,不然会出现Exception。回复请使用Response.Write(string Content)

如果发送的是Post,则使用Request.Form( "Req "),这样就获得了 "画图 "这个字符串。

你的代码使用的是Post方法来传送数据。所以接收不到是正常的。以下为Get代码的实现,Url参数直接使用http://xxx.xxx.xxx?req=画图 这样的格式就可以了。

#region HttpGet function
/// <summary>
/// Post Data to a Url by Http Get Method
/// </summary>
/// <param name= "Url "> Destination Url </param>
/// <param name= "TimeOut "> Timeout in seconds </param>
/// <returns> Content of response </returns>


private static string HttpGet(string Url, int TimeOut)
{
string rawOutput = " ";

//Prepare the WebRequest
HttpWebRequest Req = (HttpWebRequest)WebRequest.Create(Url);
Req.Timeout = TimeOut * 1000;

try
{
HttpWebResponse Resp = (HttpWebResponse)Req.GetResponse();

//Read Response
StreamReader RespReader = new StreamReader(Resp.GetResponseStream());
rawOutput = RespReader.ReadToEnd();
RespReader.Close();
}
catch (WebException Ex)
{
}

return rawOutput;
}
#endregion

热点排行