后台请求数据的几个实例(含获取数据的压缩方式的处理)

【后台请求数据的几个实例(含获取数据的压缩方式的处理)】知识就是力量,时间就是生命。这篇文章主要讲述后台请求数据的几个实例(含获取数据的压缩方式的处理)相关的知识,希望能为你提供帮助。

string url = "http://gps.cn/api/";
string param = HttpContext.Current.Request.Url.Query;
url = url + param;
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
//request.Method = "GET";
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.CookieContainer = new CookieContainer();
//request.CookieContainer.Add(new Cookie("ASP.NET_SessionId", "bqf4jp55tspvuy55ayo3gb45", "", "gps.zto.cn"));
request.CookieContainer.Add(new Cookie("ASP.NET_SessionId", ASPNET_SessionValue, "", "gps.cn"));
request.CookieContainer.Add(new Cookie("Hm_lvt_d796fdb8c57000115377e0cc55ab56f0", "1414852427", "", "gps.cn"));
request.Accept = "text/html,application/xhtml+xml,application/xml; q=0.9,*/*; q=0.8";
request.Connection = "KeepAlive";
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.Host = "gps.zto.cn";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0";
//System.Net.WebProxy proxy = new WebProxy("8.7.16.16", 80);
//request.Proxy = proxy;
//获取post来的数据再发送
byte[] data = https://www.songbingjia.com/android/Request.BinaryRead(Request.TotalBytes);
request.ContentLength = data.Length;
Stream newStream = request.GetRequestStream();
//string source = Encoding.UTF8.GetString(input);
// 发送数据
newStream.Write(data, 0, data.Length);
newStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = null;
if ("gzip".Equals(response.ContentEncoding))

responseStream = new System.IO.Compression.GZipStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress);

else if ("deflate".Equals(response.ContentEncoding))

responseStream = new System.IO.Compression.DeflateStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress);

else

responseStream = response.GetResponseStream();

if (responseStream != null)

result = new StreamReader(responseStream, Encoding.GetEncoding(response.CharacterSet)).ReadToEnd().ToString();



string url = "http://gps.cn/map";
string param = HttpContext.Current.Request.Url.Query;
url = url + param;
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "GET";
request.CookieContainer = new CookieContainer();
//request.CookieContainer.Add(new Cookie("ASP.NET_SessionId", "bqf4jp55tspvuy55ayo3gb45", "", "gps.zto.cn"));
request.CookieContainer.Add(new Cookie("ASP.NET_SessionId", ASPNET_SessionValue, "", "gps.cn"));
request.CookieContainer.Add(new Cookie("Hm_lvt_d796fdb8c57000115377e0cc55ab56f0", "1414852427", "", "gps.cn"));
request.Accept = "text/html,application/xhtml+xml,application/xml; q=0.9,*/*; q=0.8";
request.Connection = "KeepAlive";
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.Host = "gps.cn";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0";
//System.Net.WebProxy proxy = new WebProxy("8.7.16.16", 80);
//request.Proxy = proxy;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = null;
string result = string.Empty;
if ("gzip".Equals(response.ContentEncoding))

responseStream = new System.IO.Compression.GZipStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress);

else if ("deflate".Equals(response.ContentEncoding))

responseStream = new System.IO.Compression.DeflateStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress);

else

responseStream = response.GetResponseStream();

if (responseStream != null)

result = new StreamReader(responseStream, Encoding.GetEncoding(response.CharacterSet)).ReadToEnd().ToString();










    推荐阅读