1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Net; 7 8 namespace DEMO 9 { 10 public class Http_Demo 11 { 12 13 private static String LOG_TAG = "HTTPUtils"; //标记 14 private static int CONNECT_TIME_OUT = 5000; //超时 15 private static String HEADER_CONTENT_TYPE = "Content-Type"; //链接类型 16 private static String HEADER_CONTENT_LENGTH = "Content-Length"; //链接长度 17 private static String DEFAULT_PARAMS_ENCODING = "UTF-8"; //文本格式 18 19 private System.Net.HttpWebRequest Request = null; 20 private System.Net.WebResponse Response = null; 21 private System.IO.Stream Stream = null; 22 private System.IO.StreamReader Read = null; 23 private System.Byte[] Byte = null; //本身就是数组 24 private System.Text.Encoding Encode = System.Text.Encoding.UTF8; 25 private System.Text.RegularExpressions.Match Match = null; 26 27 /// <summary> 28 /// 公开属性 29 /// </summary> 30 public System.Net.WebProxy Proxy = new System.Net.WebProxy(); 31 32 public string IPFor = null; 33 public bool HideInfo = false; 34 35 /// <summary> 36 /// 初始化Request 37 /// </summary> 38 /// <param name="Request">对象</param> 39 private void init_Request(ref System.Net.HttpWebRequest Request) 40 { 41 //终端信息 42 Request.Accept = 43 "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,image/png,application/java-archive,application/java,application/x-java-archive,text/vnd.sun.j2me.app-descriptor,application/vnd.oma.drm.message,application/vnd.oma.drm.content,application/vnd.oma.dd+xml,application/vnd.oma.drm.rights+xml,application/vnd.oma.drm.rights+wbxml,application/x-nokia-widget,text/x-opml,application/vnd.nokia.headwrapper,*/*;q=0.5"; 44 Request.Headers.Add("Accept-Charset", "iso-8859-1,utf-8;q=0.7,*;q=0.7"); 45 Request.Headers.Add("Accept-Language", "zh-cn, zh;q=1.0,en;q=0.5,en;q=0.5,en;q=0.5"); 46 Request.Headers.Add("Accept-Encoding", "gzip, deflate, x-gzip, identity; q=0.9"); 47 Request.Headers.Add("X-Nokia-MusicShop-Version", "11.0842.9"); 48 //承载方式 49 Request.Headers.Add("X-Nokia-MusicShop-Bearer", "GPRS/3G"); 50 Request.Headers.Add("X-Nokia-CONNECTION_MODE", "TCP"); 51 Request.Headers.Add("X-Nokia-BEARER", "3G"); 52 Request.Headers.Add("x-up-bear-type", "GPRS/EDGE"); 53 Request.Headers.Add("X-Up-Bearer-Type", "GPRS"); 54 //GGSN信息 55 Request.Headers.Add("x-source-id", "GZGGSN1101BEr"); 56 Request.Headers.Add("Client-IP", "211.139.151.74"); 57 if (IPFor != null) Request.Headers.Add("x-forwarded-for", IPFor); 58 //网关信息 59 Request.Headers.Add("Via", "WTP/1.1 192.168.13.33 (Nokia WAP Gateway 4.1/CD1/ECD13_E/4.1.05)"); 60 Request.Headers.Add("X-Nokia-gateway-id", "NWG/4.1/Build4.1.05"); 61 //目标主机 62 //Request.Headers.Add("X-Online-Host", Request.Host); 63 //重要信息 64 if (!HideInfo) 65 { 66 Request.UserAgent = 67 "Mozilla/5.0 (SymbianOS/9.4; U; Series60/5.0 Nokia5230/10.0.055; Profile/MIDP-2.1 Configuration/CLDC-1.1 ) AppleWebKit/413 (KHTML, like Gecko) Safari/413"; 68 Request.Headers.Add("x-wap-profile", "http://nds1.nds.nokia.com/uaprof/Nokia5230r100-3G.xml"); 69 if (IPFor != null) 70 { 71 Request.Headers.Add("x-nokia-ipaddress", IPFor); 72 } 73 } 74 //自动重定向 75 Request.AllowAutoRedirect = false; 76 //代理设置 77 if (Proxy.Address != null) 78 { 79 Request.Proxy = Proxy; 80 } 81 //其它杂项 82 Request.KeepAlive = false; 83 Request.Timeout = 8000; 84 } 85 86 public string get_Internet(string url, string cookie = null) 87 { 88 try 89 { 90 Request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url); 91 url = null; 92 if (Request != null) 93 { 94 init_Request(ref Request); 95 if (cookie != null) { Request.Headers.Add("Cookie", cookie); } 96 using (Response = Request.GetResponse()) 97 { 98 Stream = Response.GetResponseStream(); 99 using (Read = new System.IO.StreamReader(Stream, System.Text.Encoding.UTF8)) 100 { 101 url = Read.ReadToEnd(); 102 } 103 Read = null; 104 Stream = null; 105 } 106 Response = null; 107 Request.Abort(); 108 Request = null; 109 } 110 return url; 111 } 112 catch (Exception ex) 113 { 114 //Error.Write(ref ex); 115 return null; 116 } 117 } 118 119 public string post_Internet(string url, Dictionary<String, String> reqParams) 120 { 121 string data = BuildHttpGetURL(url, reqParams); 122 Console.WriteLine(data); 123 124 125 string result = null; 126 try 127 { 128 Request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url); 129 if (Request != null) 130 { 131 init_Request(ref Request); //请求初始化 132 133 Request.Method = "POST"; //增加自己的属性 134 Request.ServicePoint.Expect100Continue = false; 135 136 Request.ContentType = getBodyContentType(); 137 138 Byte = Encode.GetBytes(data); 139 Request.ContentLength = Byte.Length; 140 141 using (Stream = Request.GetRequestStream()) 142 { 143 Stream.Write(Byte, 0, Byte.Length); 144 } 145 Stream = null; 146 Byte = null; 147 result = null; 148 using (Response = Request.GetResponse()) 149 { 150 Stream = Response.GetResponseStream(); 151 using (Read = new System.IO.StreamReader(Stream)) 152 { 153 result = Read.ReadToEnd(); 154 } 155 Read = null; 156 Stream = null; 157 } 158 Response = null; 159 Request.Abort(); 160 Request = null; 161 } 162 return result; 163 } 164 catch (Exception ex) 165 { 166 //Error.Write(ref ex); 167 return null; 168 } 169 } 170 171 private static String getParamsEncoding() 172 { 173 return DEFAULT_PARAMS_ENCODING; 174 } 175 176 private static String getBodyContentType() //网络链接类型 177 { 178 return "application/x-www-form-urlencoded; charset=" + getParamsEncoding(); 179 } 180 181 string BuildHttpGetURL(string baseURL, Dictionary<string, string> paramDict) //转化成为了请求的格式 182 { 183 System.Text.StringBuilder sb = new System.Text.StringBuilder(); 184 sb.Append(baseURL); 185 if (null != paramDict) 186 { 187 sb.Append("?");//添加符号 188 foreach (var pair in paramDict) 189 { 190 //TODO: urlencode 191 sb.Append("&"); 192 sb.Append(pair.Key); 193 sb.Append("="); 194 sb.Append(pair.Value); 195 } 196 } 197 return sb.ToString(); 198 } 199 } 200 }
原文:http://www.cnblogs.com/unity3d-Yang/p/6402526.html