Rocho.J

人脑是不可靠的, 随时记录感悟并且经常重复!

 

[转CSDN博客]HttpWebRequest,HttpWebResponse的用法和用途

View Code
        //1.用途:HettpWebRequest,HettpWebResponse用途和webServers的作用差不多,都是得到一个页面传过来的值。HttpWebRequest

//2。用法:
//#####################################################
//HttpWebRequest,httpWebResponse的get用法
//#####################################################

System.Net.HttpWebRequest httpWebRequest = (System.Net.HttpWebRequest)
//创建一个hettpReq请求对象,包含要传递的值name;
System.Net.HttpWebRequest.Create("hettp://hibaidu.com/yangyuefei? name=yangshuai");

httpWebRequest.Method = "GET";//传递的方法,必须要写,而已Get必须大写。

//创建一个响应对象,并重请求对象中得到响应对象的事例。
System.Net.HttpWebResponse httpWebResponse = (System.Net.HttpWebReponse)httpWebRequest.GetResponse();

//创建一个流对象(用来读取响应对象的内容)
System.IO.Stream stream = new System.IO.Stream();

//得到回应过来的流
stream = httpWebResponse.GetResponseStream();

//创建一个流的读取对象,用来输出传过来的内容
System.IO.StreamReader streamReader = new System.IO.StreamReader(stream, System.Text.Encoding.Default);

string strOut = streamReader.RedToEnd();
httpWebResponse.Close();//响应关闭,要不会占用网络资源
//输出流,要提醒的是,它输出的是 另一个网页的页面上的内容
Response.Write(strOut);


//#####################################################
//HttpWebRequest,httpWebResponse的post用法
//#####################################################

System.Net.HttpWebRequest httpWebRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create("http://httpWebRequest/httpWebRequest/httpWebRequest.aspx");

httpWebRequest.Method = "POST";
//表头的格式必须要写,否则请求响应的页面得不到要传递的值
httpWebRequest.ContentType = "application/x-www-form-urlencoded";

byte[] SomeBytes = System.Text.Encoding.Default.GetBytes("name=yang&passwod=123");//传递的值
req.ContentLength = SomeBytes.Length;
System.IO.Stream newStream = req.GetRequestStream();//把传递的值写到流中
newStream.Write(SomeBytes, 0, SomeBytes.Length);
newStream.Close();//必须要关闭 请求
System.Net.HttpWebResponse httpWebResponse = null;
httpWebResponse = (System.Net.HttpWebResponse)httpWebRequest.GetResponse();
System.IO.Stream s = httpWebResponse.GetResponseStream();
System.IO.StreamReader reader = new System.IO.StreamReader(s, System.Text.Encoding.Default);
string respHTML = reader.ReadToEnd();

Response.Write(respHTML);

s.Close();
 
//================================异步请求============================
   public static ManualResetEvent allDone = new ManualResetEvent(false);
        static void Main(string[] args)
        {          
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.contoso.com/example.aspx");

            request.ContentType = "application/x-www-form-urlencoded";
            request.Method = "POST";  
            request.BeginGetRequestStream(new AsyncCallback(ReadCallback), request);

            allDone.WaitOne();

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream streamResponse = response.GetResponseStream();
            StreamReader streamRead = new StreamReader(streamResponse);
            string responseString = streamRead.ReadToEnd();

            streamResponse.Close();
            streamRead.Close();
            response.Close();
            Console.WriteLine(responseString);
            Console.ReadKey();
        }
        private static void ReadCallback(IAsyncResult asynchronousResult)
        {
            HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
            Stream postStream = request.EndGetRequestStream(asynchronousResult);
            Console.WriteLine("Please enter the input data to be posted:");
            string postData = Console.ReadLine();

            byte[] byteArray = Encoding.UTF8.GetBytes(postData);  
           postStream.Write(byteArray, 0, postData.Length);
             postStream.Close();    
         allDone.Set();    
     }

 

 

posted on 2012-01-13 11:39  RJ  阅读(273)  评论(0编辑  收藏

导航