c#httpwebrequest [英] httpwebrequest in c#

查看:87
本文介绍了c#httpwebrequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我发现此代码发送http请求。



hi i found this code to send http request.

string url="http://www.contoso.com/default.html";
       string requestData = "";
       string RequestMethod = "POST";
       protected void Page_Load(object sender, EventArgs e)
       {
           try
           {
               /* Create a request using a URL that can receive a post*/
               HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
               /*set header*/
               request.Method = RequestMethod;
               //request.UserAgent="Mozilla/5.0";
               //request.Accept="";
               //request.Connection="";
               //request.ContentLength=requestData.Length;
               //request.ContentType="application/x-www-form-urlencoded";
               //request.Date="";
               //request.Expect="";
               //request.Host="";
               //request.Referer="";
               //request.TransferEncoding="";
               //request.Proxy=null;
               //request.Timeout="";
               //request.Credentials = CredentialCache.DefaultCredentials;
               //*********************
               Byte[] postDataByte = Encoding.ASCII.GetBytes(requestData);
               /* Get the request stream.*/
               Stream requestStream = request.GetRequestStream();
               /*Write the data to the request stream*/
               requestStream.Write(postDataByte, 0, postDataByte.Length);
               /*Close the Stream object*/
               requestStream.Close();
               /*To send the request to the server, call GetResponse*/
               HttpWebResponse response = (HttpWebResponse)request.GetResponse();
               /*Get Some Respone info*/
               Response.Write(((HttpWebResponse)response).StatusDescription);
               Response.Write(((HttpWebResponse)response).Headers);
               /*Get the stream containing content returned by the server*/
               Stream dataStream = response.GetResponseStream();
               /*Open the stream using a StreamReader for easy access*/
               StreamReader reader = new StreamReader(dataStream);
               /* Read the content.*/
               string responseFromServer = reader.ReadToEnd();
               /*Display the content.*/
               Response.Write(responseFromServer);
               /*Clean up the streams*/
               reader.Close();
               dataStream.Close();
               response.Close();
           }
           catch (Exception)
           {
               Response.Write("An error occured");
           }
       }





问题是我不知道为什么这部分代码是必要的



the problem is i dont know why this part of code is necessary

Byte[] postDataByte = Encoding.ASCII.GetBytes(requestData);
                /* Get the request stream.*/
                Stream requestStream = request.GetRequestStream();
                /*Write the data to the request stream*/
                requestStream.Write(postDataByte, 0, postDataByte.Length);
                /*Close the Stream object*/
                requestStream.Close();





为什么我需要获取请求流。



why i need to get request stream.

推荐答案

这是指向 WebRequest.GetRequestStream方法 [ ^ ]。它包含在:



Here is the link to the WebRequest.GetRequestStream Method [^] in MSDN. It is covered as:

Quote:

GetRequestStream方法发起请求将数据发送到Internet资源并返回Stream实例以将数据发送到Internet资源。



GetRequestStream方法提供对Stream的同步访问。对于异步访问,请使用BeginGetRequestStream和EndGetRequestStream方法。

The GetRequestStream method initiates a request to send data to the Internet resource and returns a Stream instance for sending data to the Internet resource.

The GetRequestStream method provides synchronous access to the Stream. For asynchronous access, use the BeginGetRequestStream and EndGetRequestStream methods.





此外,您应该记住,当流关闭时,帖子会被触发。获取流请求不会触发它。



祝你好运,

OI



Also you should keep in mind that the post is getting triggered when the stream is closed. Getting the stream request does not triggers it.

Good luck,
OI


这篇关于c#httpwebrequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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