HttpWebRequest的简单使用
Posted 叶丶梓轩
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HttpWebRequest的简单使用相关的知识,希望对你有一定的参考价值。
新建新的空网站和一个default.aspx页面测试,实验例子:
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { HttpWebRequest web = (HttpWebRequest)WebRequest.Create("http://www.cnblogs.com/"); //创建一个请求 web.Method = "GET"; //方式为get web.ContentType = "text/html;charset=UTF-8"; //定义获取数据的类型 HttpWebResponse response = (HttpWebResponse)web.GetResponse(); //获取响应 string result = ""; using (Stream stream = response.GetResponseStream()) //定义缓存,将数据读取出来 { StreamReader sr = new StreamReader(stream); result = sr.ReadToEnd(); } Label1.Text = result.ToString(); } }
二,客户端带参和带文件流的请求方法
#region HTTP请求 /// <summary> /// HTTP请求接口 /// </summary> /// <param name="UploadFile"></param> /// <returns></returns> public ActionResult HZRequest2(HttpPostedFileBase UploadFile) { string key = "PKWodTrwuTXoMvRUZhUgyf"; string url = "http://localhost:5832/Home/HZDistinguish?key=" + key + ""; byte[] byt = new byte[UploadFile.InputStream.Length]; UploadFile.InputStream.Read(byt, 0, (int)UploadFile.InputStream.Length); //初始化新的webRequst //1. 创建httpWebRequest对象 HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url); //2. 初始化HttpWebRequest对象 webRequest.Method = "POST"; webRequest.ContentType = "multipart/form-data"; webRequest.ContentLength = byt.Length; //3. 附加要POST给服务器的数据到HttpWebRequest对象(附加POST数据的过程比较特殊,它并没有提供一个属性给用户存取,需要写入HttpWebRequest对象提供的一个stream里面。) Stream newStream = webRequest.GetRequestStream();//创建一个Stream,赋值是写入HttpWebRequest对象提供的一个stream里面 newStream.Write(byt, 0, byt.Length); newStream.Close(); //4. 读取服务器的返回信息 HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse(); StreamReader stmRead = new StreamReader(response.GetResponseStream(), Encoding.UTF8); string reapond = stmRead.ReadToEnd(); response.Close(); stmRead.Close(); return Content(reapond); } #endregion
服务器接收方法:
public ActionResult HZDistinguish() { int lang = Request.TotalBytes; byte[] bytes = Request.BinaryRead(lang); Stream stream = Request.InputStream; string key = Request["key"]; }
三,Post带参数的方法
public static string Post(string url, Dictionary<string, object> dic) { StringBuilder str = new StringBuilder(); foreach (KeyValuePair<string, object> kv in dic) { string pkey = kv.Key; object pvalue = kv.Value; str.Append("&" + pkey + "=" + pvalue); } var data = Encoding.ASCII.GetBytes(str.ToString()); //初始化新的webRequst //1. 创建httpWebRequest对象 HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url); //2. 初始化HttpWebRequest对象 webRequest.Method = "Post"; webRequest.ContentType = "application/x-www-form-urlencoded"; webRequest.ContentLength = data.Length; using (var stream = webRequest.GetRequestStream()) { stream.Write(data, 0, data.Length); } //4. 读取服务器的返回信息 HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse(); StreamReader stmRead = new StreamReader(response.GetResponseStream(), Encoding.UTF8); string result = stmRead.ReadToEnd(); response.Close(); stmRead.Close(); return result; }
以上是关于HttpWebRequest的简单使用的主要内容,如果未能解决你的问题,请参考以下文章
C#使用HttpWebRequest发送数据和使用HttpWebResponse接收数据的一个简单示例