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

Posted hnsongbiao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了后台请求数据的几个实例(含获取数据的压缩方式的处理)相关的知识,希望对你有一定的参考价值。

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 = 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();





以上是关于后台请求数据的几个实例(含获取数据的压缩方式的处理)的主要内容,如果未能解决你的问题,请参考以下文章

请求后台数据的几种方式

layui页面操作,点击一个添加页面,跳转有确定,然后点击确定后将选择的几个数据返回前一个页面获取值,然后ajax请求后台

Java在前台怎么获取后台的数据

JavaScript请求后台数据的几种常用方式 #yyds干货盘点#

php的curl的几个实例

浅析进程间通信的几种方式(含实例源码)