標籤:winform code http ext get string
//data:參數 URL:路徑
public static string PostWebRequest(string Data, string URL)
{
CookieContainer cc = new CookieContainer();
string postData = Data;
byte[] byteArray = Encoding.UTF8.GetBytes(postData); // 轉化
HttpWebRequest webRequest2 = (HttpWebRequest)WebRequest.Create(new Uri(URL));
webRequest2.CookieContainer = cc;
webRequest2.Method = "POST";
webRequest2.ContentType = "application/x-www-form-urlencoded";
webRequest2.ContentLength = byteArray.Length;
Stream newStream = webRequest2.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length); //寫入參數
newStream.Close();
HttpWebResponse response2 = (HttpWebResponse)webRequest2.GetResponse();
StreamReader sr2 = new StreamReader(response2.GetResponseStream(), Encoding.UTF8);
string text2 = sr2.ReadToEnd();
if (text2 != null && text2.Length > 0)
{
return text2;
}
return "";
}