C# Http请求(WebClient,HttpWebRequest)
来源:互联网 发布:论文:浅析淘宝店的运营 编辑:程序博客网 时间:2024/05/22 10:42
WebClient Post Get提交数据
using (WebClient client = new WebClient()){ client.Headers["version"] = "1.0";//http header 填入属性 client.Headers["APIToken"] = token; string datas = "sdfdsfsdfsf";//Post提交的数据 string strs = GetStrs(order.OrderId);//获取xml文档 //resul是返回的结果 string resul = System.Text.Encoding.UTF8.GetString(client.UploadData("请求地址", "POST", Encoding.UTF8.GetBytes(datas))); //Client.DownloadFile(url, paths);//url地址,paths写入文件的地址}1. string postString = "arg1=a&arg2=b";//这里即为传递的参数,可以用工具抓包分析,也可以自己分析,主要是form里面每一个name都要加进来
2. byte[] postData = Encoding.UTF8.GetBytes(postString);//编码,尤其是汉字,事先要看下抓取网页的编码方式
3. string url = "http://localhost/register.php";//地址
4. WebClient webClient = new WebClient();
5. webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");//采取POST方式必须加的header,如果改为GET方式的话就去掉这句话即可
client.Headers.Add("Content-Type", "application/x-www-form-urlencoded;charset=utf-8" );
6. byte[] responseData = webClient.UploadData(url, "POST", postData);//得到返回字符流
7. string srcString = Encoding.UTF8.GetString(responseData);//解码
获取返回500失败的信息 响应的信息
服务端程序产生致命错误,http 表现为错误号 500,服务器拒绝响应
代码
try { using (WebClient client = new WebClient()) { client.Headers.Add( "Content-Type", "application/x-www-form-urlencoded" ); string result = Encoding .UTF8.GetString(client.UploadData(Helper.CountryUrl, Encoding.UTF8.GetBytes(Helper .ReaquestCountryData()))); XmlDocument document = new XmlDocument(); document.LoadXml(result); XmlNodeList xnlis = document.DocumentElement.ChildNodes[0].ChildNodes[0].ChildNodes; List<Service.country > list = new List<Service. country>(); foreach (XmlNode x in xnlis) { list.Add( new Service.country () { enName = x.GetAttributeValue("enName") }); } return list.ToArray(); } } catch(WebException e) { Stream stream = e.Response.GetResponseStream(); byte[] buf = new byte[e.Response.ContentLength]; stream.Read(buf, 0, buf.Length); string str = Encoding .UTF8.GetString(buf); }//HttpWebRequest
//Get请求
public static string GetUrltoHtml(string Url,string type) { try { System.Net.WebRequest wReq = System.Net.WebRequest.Create(Url); // Get the response instance. System.Net.WebResponse wResp = wReq.GetResponse(); System.IO.Stream respStream = wResp.GetResponseStream(); // Dim reader As StreamReader = New StreamReader(respStream) using (System.IO.StreamReader reader = new System.IO.StreamReader(respStream, Encoding.GetEncoding(type))) { return reader.ReadToEnd(); } } catch (System.Exception ex) { //errorMsg = ex.Message; } return ""; }//Post请求
///<summary> ///采用https协议访问网络 ///</summary> ///<param name="URL">url地址</param> ///<param name="strPostdata">发送的数据</param> ///<returns></returns> public string OpenReadWithHttps(string URL, string strPostdata, string strEncoding) { Encoding encoding = Encoding.Default; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL); request.Method = "post"; request.Accept = "text/html, application/xhtml+xml, */*"; request.ContentType = "application/x-www-form-urlencoded"; byte[] buffer = encoding.GetBytes(strPostdata); request.ContentLength = buffer.Length; request.GetRequestStream().Write(buffer, 0, buffer.Length); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using( StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding(strEncoding))) { return reader.ReadToEnd(); } }
0 0
- C# Http请求(WebClient,HttpWebRequest)
- C# HttpWebRequest\HttpWebResponse\WebClient发送请求解析json数据
- C# Get请求数据 WebClient和HttpWebRequest获取HTML代码
- C#通过WebClient/HttpWebRequest实现http的post/get方法
- C#通过WebClient/HttpWebRequest实现http的post/get方法
- C#通过WebClient/HttpWebRequest实现http的post/get方法
- C#通过WebClient/HttpWebRequest实现http的post/get方法
- C# 发送Http请求 - WebClient类
- c# HttpWebRequest post和get请求http
- c# 使用HttpWebRequest发送http请求
- C# HttpWebRequest GET HTTP HTTPS 请求
- Http请求之 WebClient
- Http请求之--C#的HttpWebRequest实现POST方式请求
- C#通过WebClient/HttpWebRequest实现http的post/get方法与java对接
- C#通过WebClient/HttpWebRequest实现http的post/get方法与java对接
- HttpWebRequest 模拟Http请求
- 在C#用HttpWebRequest中发送GET/HTTP/HTTPS请求
- 在C#用HttpWebRequest中发送GET/HTTP/HTTPS请求
- ifconfig 说明
- graph slam tutorial : g2o 的使用
- iOS百度地图poi检索(基于百度地图2.3.0SDK开发)
- 大数据系列第四课:scala高阶
- 安卓 SharedPreferences 的简单使用
- C# Http请求(WebClient,HttpWebRequest)
- hibernate中联合主键总结_annotation
- Java中,&&与&,||与|的区别
- centos 7 samba
- 关系型数据库基础 one
- java里&&与&的区别
- ios百度地图的使用(基本定位和地理编码)
- ios百度地图逆地理编码(基于2.3.0--2.4.0SDK开发)
- GitHub搜索小技巧