HttpRequest与HttpResponse用法助解
c# HttpWebRequest 和HttpWebResponse
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using System.Net;
namespace WebRequestToLogin { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void button1_Click(object sender, EventArgs e) { string Txt=""; Login(UserName.Text, UserPwd.Text, LoginUrl.Text, out Txt);
//获取登录后的页面数据
textBox2.Text = Txt;
//判断该页面数据是否是登陆后的数据就可以判断是否登录成功了
//由于都是字符串操作就没有必要写了呵呵 }
public void Login(string UserName, string UserPwd, string LoginUrl, out string Txt) { try { //定义Cookie容器 CookieContainer CookieArray = new CookieContainer();
//创建Http请求 HttpWebRequest LoginHttpWebRequest = (HttpWebRequest)WebRequest.Create(LoginUrl);
//登录数据 string LoginData = "email=" + UserName + "&password=" + UserPwd; //数据被传输类型 LoginHttpWebRequest.ContentType = "application/x-www-form-urlencoded"; //数据长度 LoginHttpWebRequest.ContentLength = LoginData.Length; //数据传输方法 get或post LoginHttpWebRequest.Method = "POST"; //设置HttpWebRequest的CookieContainer为刚才建立的那个CookieArray LoginHttpWebRequest.CookieContainer = CookieArray; //获取登录数据流 Stream myRequestStream = LoginHttpWebRequest.GetRequestStream(); //StreamWriter StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.Default); //把数据写入HttpWebRequest的Request流 myStreamWriter.Write(LoginData);
//关闭打开对象 myStreamWriter.Close();
myRequestStream.Close();
//新建一个HttpWebResponse HttpWebResponse myHttpWebResponse = (HttpWebResponse)LoginHttpWebRequest.GetResponse();
//获取一个包含url的Cookie集合的CookieCollection myHttpWebResponse.Cookies = CookieArray.GetCookies(LoginHttpWebRequest.RequestUri);
WebHeaderCollection a = myHttpWebResponse.Headers;
Stream myResponseStream = myHttpWebResponse.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.Default);
Txt = myStreamReader.ReadToEnd();
//把数据从HttpWebResponse的Response流中读出 myStreamReader.Close();
myResponseStream.Close(); } catch (Exception ex) { throw ex; } }
} }
解释二:
1.用途:HettpWebRequest,HettpWebResponse用途和webServers的作用差不多,都是得到一个页面传过来的值。HttpWebRequest
2。用法:----------------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();