C#中關於Send WebRequest 方法與資料處理
很久不更新部落格了,這次大概說一下C#中的Send WebRequest,眾所周知,.NET網路程式設計在測試中是非常常見的,具體來說,例如我們如果測試一個API,透過Send WebRequest的方式來進行API層面的功能測試,然後用Selenium完成前端的驗證,這個模式應該說是比較方便而通用的,具體以其中最常見的HTTP request為例,當我們需要傳輸資料時,需要
1)位址: Address作為Webrequest參數實例化,然後設定對應屬性,例如Method(Get還是Post等等)
2)資料:Data作為HttpRequest的資料流寫入:
XmlResponse = null;
ResponseCode = HttpStatusC. ServerResponse = String.Empty;
參數處理部分
//Format the data
string output = data.Aggregate(String.Empty, (current, keyValuePair) => current + ("&" + ValuePair.Key + HttpUtility.UrlEncode(keyValuePair.Value)));
var encoding = new UTF8Encoding();
//設定PostData變數將output處理,最後得到byte[] d Encoding.ASCII.GetBytes(output);
byte[] d = encoding.GetBytes(output.Substring(1, output.Length - 1); GET部分的資料處理
if (method.ToUpperInvariant() == "GET")
address=address+output
//Add post process
if ( request == null)
throw new Exception("WebRequest object is null.");
request.Method = "POST";
request.ContentType = "application/xwww-form-urlencode. = d.Length;
ServerResponse = String.Empty;
//資料讀取與處理部分
using (Stream dataStream = request.GetRequestStream())
. ;
dataStream.Close();
using (var response = (HttpWebResponse)request.GetResponse ode = response.StatusCode;
using (Stream myStream = response.GetResponseStream())
if ( myStream != null)
var readStream = new StreamReader(myStream, Encoding.UTF8);
};
} response.Close();
if ( ResponseCode == HttpStatusCode.OK)
{
LoadXml();
返回 false;

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)