c# 裡 HttpWebRequest 裡有些header(常用的) 都有內建屬性可以直接設定
例如Accept或Referer都可以像下面這樣設定:
hreq.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,..@!$#&";
那如果你要的header預設的函式庫裡找不到 例如 Accept-Language
只好就直接像這樣設定:
hreq.Headers.Set("Accept-Language", "zh-tw");
設request body的話網路找應該都一堆範例
大致上就是要先編碼過然後再送出去 整個流程大概長這樣
HttpWebRequest hreq = (HttpWebRequest)HttpWebRequest.Create(url);
hreq.Method = "POST";
//
//設定前面說的你想設的headers
//
string postData = "aaa=123&bbb=456";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
Stream newStream = hreq.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length);
newStream.Close();
HttpWebResponse response = (HttpWebResponse)hreq.GetResponse();
收工~ response就是從web取回的資料了
例如Accept或Referer都可以像下面這樣設定:
hreq.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,..@!$#&";
那如果你要的header預設的函式庫裡找不到 例如 Accept-Language
只好就直接像這樣設定:
hreq.Headers.Set("Accept-Language", "zh-tw");
設request body的話網路找應該都一堆範例
大致上就是要先編碼過然後再送出去 整個流程大概長這樣
HttpWebRequest hreq = (HttpWebRequest)HttpWebRequest.Create(url);
hreq.Method = "POST";
//
//設定前面說的你想設的headers
//
string postData = "aaa=123&bbb=456";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
Stream newStream = hreq.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length);
newStream.Close();
HttpWebResponse response = (HttpWebResponse)hreq.GetResponse();
收工~ response就是從web取回的資料了
0 意見:
張貼留言張貼留言