C#通過GET/POST方式發(fā)送Http請求
介紹http請求的兩種方式,get和post方式。并用C#語言實(shí)現(xiàn),如何請求url并獲取返回的數(shù)據(jù)
兩者的區(qū)別:
參數(shù)
Get請求把提交的數(shù)據(jù)進(jìn)行簡單編碼,同時將url的一部分發(fā)送到服務(wù)器
比如url:Http://127.0.0.1/login.jsp?Name=zhangshi&Age=30&Submit=%cc%E+%BD%BB
所以get請求方式提交的數(shù)據(jù)存在一定的安全隱患,如果在使用對安全性要求教高的操作(比如用戶登錄,支付),應(yīng)使用post方式。Get請求是默認(rèn)的http請求方法,我們一般通過get方法來獲取表單數(shù)據(jù)
POST請求會把請求的數(shù)據(jù)放置在HTTP請求包的包體中。上面的item=bandsaw就是實(shí)際的傳輸數(shù)據(jù)。
傳輸數(shù)據(jù)的大小
GET,特定的瀏覽器和服務(wù)器對URL的長度有限制。因此,在使用GET請求時,傳輸數(shù)據(jù)會受到URL長度的限制。
POST,由于不是URL傳值,理論上是不會受限制的,但是實(shí)際上各個服務(wù)器會規(guī)定對POST提交數(shù)據(jù)大小進(jìn)行限制,Apache、IIS都有各自的配置。
安全性
POST的安全性比GET的高。這里的安全是指真正的安全,而不同于上面GET提到的安全方法中的安全,上面提到的安全僅僅是不修改服務(wù)器的數(shù)據(jù)。比如,在進(jìn)行登錄操作,通過GET請求,用戶名和密碼都會暴露再URL上,因?yàn)榈卿涰撁嬗锌赡鼙粸g覽器緩存以及其他人查看瀏覽器的歷史記錄的原因,此時的用戶名和密碼就很容易被他人拿到了。除此之外,GET請求提交的數(shù)據(jù)還可能會造成Cross-site request frogery攻擊
HTTP中的GET,POST,SOAP協(xié)議都是在HTTP上運(yùn)行的
Get請求
請求類
///
/// Get請求
///
///
/// 字符串
public static string GetHttpResponse(string url, int Timeout)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8";
request.UserAgent = null;
request.Timeout = Timeout;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
return retString;
}
調(diào)用方法
string url="Http://127.0.0.1/login.jsp?Name=zhangshi&Age=30&Submit=%cc%E+%BD%BB";
string res = HttpHelper.GetHttpResponse(url, 6000);
if (res != null)
{
T mes = JsonHelper.DeserializeJsonToObject<T>(res)
}
Post請求
/// 創(chuàng)建POST方式的HTTP請求
public static HttpWebResponse CreatePostHttpResponse(string url, IDictionary<string, string> parameters, int timeout, string userAgent, CookieCollection cookies)
{
HttpWebRequest request = null;
//如果是發(fā)送HTTPS請求
if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
{
request = WebRequest.Create(url) as HttpWebRequest;
}
else
{
request = WebRequest.Create(url) as HttpWebRequest;
}
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
//設(shè)置代理UserAgent和超時
//request.UserAgent = userAgent;
//request.Timeout = timeout;
if (cookies != null)
{
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(cookies);
}
//發(fā)送POST數(shù)據(jù)
if (!(parameters == null || parameters.Count == 0))
{
StringBuilder buffer = new StringBuilder();
int i = 0;
foreach (string key in parameters.Keys)
{
if (i > 0)
{
buffer.AppendFormat("&{0}={1}", key, parameters[key]);
}
else
{
buffer.AppendFormat("{0}={1}", key, parameters[key]);
i++;
}
}
byte[] data = Encoding.ASCII.GetBytes(buffer.ToString());
using (Stream stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
}
string[] values = request.Headers.GetValues("Content-Type");
return request.GetResponse() as HttpWebResponse;
}
/// <summary>
/// 獲取請求的數(shù)據(jù)
/// </summary>
public static string GetResponseString(HttpWebResponse webresponse)
{
using (Stream s = webresponse.GetResponseStream())
{
StreamReader reader = new StreamReader(s, Encoding.UTF8);
return reader.ReadToEnd();
}
}
調(diào)用方法
//參數(shù)p
IDictionary<string, string> parameters = new Dictionary<string, string>();
parameters.Add("p", HttpUtility.UrlEncode(p));
//http請求
System.Net.HttpWebResponse res = HttpHelper.CreatePostHttpResponse(url, parameters, 3000, null, null);
if (res == null)
{
Response.Redirect("RequestFailed.aspx?result=出錯了,可能是由于您的網(wǎng)絡(luò)環(huán)境差、不穩(wěn)定或安全軟件禁止訪問網(wǎng)絡(luò),您可在網(wǎng)絡(luò)好時或關(guān)閉安全軟件在重新訪問網(wǎng)絡(luò)。");
}
else
{
//獲取返回數(shù)據(jù)轉(zhuǎn)為字符串
string mes = HttpHelper.GetResponseString(res);
T model = JsonHelper.DeserializeJsonToObject<T>(mes);
}
到此這篇關(guān)于C#通過GET/POST方式發(fā)送Http請求的文章就介紹到這了,更多相關(guān)C# 發(fā)送Http請求內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#調(diào)用AForge實(shí)現(xiàn)攝像頭錄像的示例代碼
這篇文章主要介紹了C#調(diào)用AForge實(shí)現(xiàn)攝像頭錄像的示例代碼,非常具有實(shí)用價值,需要的朋友可以參考下2017-09-09
C#網(wǎng)站生成靜態(tài)頁面的實(shí)例講解
今天小編就為大家分享一篇關(guān)于C#網(wǎng)站生成靜態(tài)頁面的實(shí)例講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-01-01
C#使用OleDb操作Excel和數(shù)據(jù)庫的策略
在C#編程中,使用OleDb可以方便地實(shí)現(xiàn)對Excel文件和數(shù)據(jù)庫的操作,本文探討了在C#中使用OleDb技術(shù)操作Excel和數(shù)據(jù)庫的策略,文章詳述了OleDb的定義、配置環(huán)境的步驟,并通過實(shí)際代碼示例演示了如何高效讀寫Excel文件和交互數(shù)據(jù)庫,需要的朋友可以參考下2024-05-05
12306奇葩驗(yàn)證碼引發(fā)思考之C#實(shí)現(xiàn)驗(yàn)證碼程序
春運(yùn)最高峰來了!明天通過網(wǎng)絡(luò)將能買到小年夜的車票,本周四就將開售除夕日車票,但不少人被首次在春運(yùn)期間使用的圖片驗(yàn)證碼搞得很火大,小編也正在對驗(yàn)證碼進(jìn)行研究,編寫了由C#實(shí)現(xiàn)驗(yàn)證碼程序,分享給大家2015-12-12

