简介 HttpWebRequest 是 .net 基类库中的一个类,也在 UWP 中有所体现。它在命名空间 System.Net 之下,可以使用 HTTP 协议和服务器交互。但是 UWP 中的 HttpWebRequest 同之前.Net 中的还是有一定的区别,少了许多方法。
HttpWebRequest 对 HTTP 协议封装的比较完备,可以设置 Header 和 Content,也可以自行设置 Cookie。
多数 Http 访问以 Get 和 Post 为主,所以在这里也是对 Get 和 Post 两种方法进行封装。
Http Get 要使用 Get 进行一个链接的访问,最基本的 url,Get 的参数和网页的编码形式需要以参数形式输入,最终一般只需要拿到返回的 Content 即可。
以下是实现的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 public static async Task<string > HttpGet (string url, string getDataStr = null , string encode = "utf-8" ,CookieCollection cc = null ) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + (getDataStr == null ? "" : "?" ) + getDataStr); request.CookieContainer = new CookieContainer(); if (cc != null ) { request.CookieContainer.Add(new Uri(url), cc); } request.Method = "GET" ; request.ContentType = "text/html;charset=UTF-8" ; HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync(); Stream myResponseStream = response.GetResponseStream(); Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding(encode)); string retString = myStreamReader.ReadToEnd(); return retString; }
Http Post 使用 Post 发送数据时候需要将参数以流的形式写入 Request 的 Header 中,其余和 Get 非常相似。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 public static async Task<string> HttpPost(string url, string postDataStr, string encode = "utf-8", CookieCollection cc = null ) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create (url); request.CookieContainer = new CookieContainer(); if (cc != null ) { request.CookieContainer.Add (new Uri(url), cc); } //设置请求方式为POST request.Method = "POST"; //在POST里一定要注意写入Content—Length,这里的长度是指POST上传的数据的长度,可以使用Encoding 中的GetByteCount方法完成 request.Headers["Content-Length"] = Encoding .UTF8.GetByteCount(postDataStr).ToString(); //ContentType设置为Web表单模式 request.ContentType = "application/x-www-form-urlencoded"; //test request header //request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; //request.Headers["Accept-Encoding"] = "gzip, deflate"; //request.Headers["Accept-Language"] = "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3"; //request.Headers["Connection"] = "keep-alive"; //request.Headers["DNT"] = "1"; //request.Headers["Host"] = "10.3.8.211"; //request.Headers["Referrer"] = "http://10.3.8.211/"; //request.Headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0"; //拿到request的输入流 Stream myRequestStream = await request.GetRequestStreamAsync(); //use this function to register the encoding machine or it will throw expect Encoding .RegisterProvider(CodePagesEncodingProvider.Instance); //I Don't know why it doesn' t work , so I use direct function to write //StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding .GetEncoding(encode)); //myStreamWriter.Write (postDataStr); //将传输的数据转化为ASCII码写入输入流 byte[] bs = Encoding .ASCII.GetBytes(postDataStr); myRequestStream.Write (bs, 0 , bs.Length); //异步得到Response并且将Response转换为String HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding .GetEncoding(encode)); string retString = myStreamReader.ReadToEnd(); return retString; }
获取文中代码 在 GitHub 的 HelloBUPT 项目中可以得到代码:https://github.com/imaginezz/helloBUPT/blob/master/beiyou/CommonLibrary/WebLib.cs
参考 msdn 文档:https://msdn.microsoft.com/zh-cn/library/mt185489.aspx