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


                    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();
                }
  • 相关阅读:
    C++ crash 堆栈信息获取(三篇文章)
    qt捕获全局windows消息(使用QAbstractNativeEventFilter,然后注册这个类)
    qt之fillder抓包(QT网络版有一些具体的坑)
    qt之窗口换肤(一个qss的坑:当类属性发现变化时需要重置qss,使用rcc资源文件)
    模块化开发AraeRegistration
    CLR的组成和运转
    开源框架Caliburn.Micro
    URL
    负载均衡架构
    C add
  • 原文地址:https://www.cnblogs.com/hnsongbiao/p/4779507.html
Copyright © 2020-2023  润新知