none
c# winform 模拟请求网址 ,更改head头,获取返回数据 RRS feed

  • 问题

  • 模拟地址:http://ocean.cnss.com.cn/index.php?m=resource&c=tide&a=get_tide_data&portid=68&date=2014-07-22

    直接请求地址是不能获取的json数据的

    要更改请求的head头信息,可能还有cookie信息,才能获取到json数据,

    可以下载fireFox浏览器重新编辑head头信息,这样就可以获取到json数据.

    我想用c# winfrom模拟这个功能,更改head头,然后返回json数据给我。

    head头信息如下:

    Host: ocean.cnss.com.cn
    User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0
    Accept: application/json, text/javascript, */*; q=0.01
    Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3
    Accept-Encoding: gzip, deflate
    X-Requested-With: XMLHttpRequest
    Referer: http://ocean.cnss.com.cn/
    Cookie: PHPSESSID=bia752fnm0681mr4jhnsmrde94; bdshare_firstime=1405997786686; Hm_lvt_9031c4bc2cecacb6d760cb393192453e=1405997787; Hm_lpvt_9031c4bc2cecacb6d760cb393192453e=1406005239; _ga=GA1.3.1567846960.1405997784
    Connection: keep-alive

    现在怎么也获取不了,不知道是不是cookie的原因


    我要飞得更远!

    2014年7月22日 9:46

全部回复

  • 参考代码:using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Net;
    using System.IO;
    using System.Collections.Specialized;
    using System.Web;

    namespace test
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                Uri uri = new Uri(this.txt_url.Text.Trim());
                string queryString = uri.Query;
                NameValueCollection col = GetQueryString(queryString);
                this.textBox1.Text=HttpPost(this.txt_url.Text.Trim(), col);
            }


            /// <summary>
            /// 将查询字符串解析转换为名值集合.
            /// </summary>
            /// <param name="queryString"></param>
            /// <returns></returns>
            public static NameValueCollection GetQueryString(string queryString)
            {
                return GetQueryString(queryString, null, true);
            }

            /// <summary>
            /// 将查询字符串解析转换为名值集合.
            /// </summary>
            /// <param name="queryString"></param>
            /// <param name="encoding"></param>
            /// <param name="isEncoded"></param>
            /// <returns></returns>
            public static NameValueCollection GetQueryString(string queryString, Encoding encoding, bool isEncoded)
            {
                queryString = queryString.Replace("?", "");
                NameValueCollection result = new NameValueCollection(StringComparer.OrdinalIgnoreCase);
                if (!string.IsNullOrEmpty(queryString))
                {
                    int count = queryString.Length;
                    for (int i = 0; i < count; i++)
                    {
                        int startIndex = i;
                        int index = -1;
                        while (i < count)
                        {
                            char item = queryString[i];
                            if (item == '=')
                            {
                                if (index < 0)
                                {
                                    index = i;
                                }
                            }
                            else if (item == '&')
                            {
                                break;
                            }
                            i++;
                        }
                        string key = null;
                        string value = null;
                        if (index >= 0)
                        {
                            key = queryString.Substring(startIndex, index - startIndex);
                            value = queryString.Substring(index + 1, (i - index) - 1);
                        }
                        else
                        {
                            key = queryString.Substring(startIndex, i - startIndex);
                        }
                        if (isEncoded)
                        {
                            result[MyUrlDeCode(key, encoding)] = MyUrlDeCode(value, encoding);
                        }
                        else
                        {
                            result[key] = value;
                        }
                        if ((i == (count - 1)) && (queryString[i] == '&'))
                        {
                            result[key] = string.Empty;
                        }
                    }
                }
                return result;
            }

            /// <summary>
            /// 解码URL.
            /// </summary>
            /// <param name="encoding">null为自动选择编码</param>
            /// <param name="str"></param>
            /// <returns></returns>
            public static string MyUrlDeCode(string str, Encoding encoding)
            {
                if (encoding == null)
                {
                    Encoding utf8 = Encoding.UTF8;
                    //首先用utf-8进行解码                     
                    string code = HttpUtility.UrlDecode(str.ToUpper(), utf8);
                    //将已经解码的字符再次进行编码.
                    string encode = HttpUtility.UrlEncode(code, utf8).ToUpper();
                    if (str == encode)
                        encoding = Encoding.UTF8;
                    else
                        encoding = Encoding.GetEncoding("gb2312");
                }
                return HttpUtility.UrlDecode(str, encoding);
            }

            public string HttpPost(string url, NameValueCollection nvc)
            {
                HttpWebRequest requestScore = (HttpWebRequest)WebRequest.Create(url);
                ASCIIEncoding encoding = new ASCIIEncoding();

                Encoding myEncoding = Encoding.GetEncoding("UTF-8");
                UTF8Encoding utf8 = new UTF8Encoding();
                //string postPic = "";
                //FileInfo fl = new FileInfo(m_PicFullName);
                //postPic += ("type=" + "image/jpeg");
                //postPic += ("&name=" + utf8.GetString(utf8.GetBytes(m_PicFullName)));
                //postPic += ("&data=" + "127.0.0.1");
                string postData = "";
                foreach (string key in nvc.Keys)
                {
                    if (postData == "")
                        postData += (key + "=" + nvc[key]);
                    else
                        postData += ("&" + key + "=" + nvc[key]);
                    ;
                }

                byte[] data = myEncoding.GetBytes(postData);
                requestScore.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0";
                requestScore.Accept = "application/json, text/javascript, */*; q=0.01";
                requestScore.Headers.Add("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
                requestScore.Headers.Add("Accept-Encoding", "gzip, deflate");
                //requestScore.Headers.Add("Cookie", "PHPSESSID=bia752fnm0681mr4jhnsmrde94; bdshare_firstime=1405997786686; Hm_lvt_9031c4bc2cecacb6d760cb393192453e=1405997787; Hm_lpvt_9031c4bc2cecacb6d760cb393192453e=1406005239; _ga=GA1.3.1567846960.1405997784");
                requestScore.Method = "Post";
               // requestScore.ContentType = "text/html; charset=utf-8";
                //requestScore.ContentLength = 230;
                requestScore.KeepAlive = true;

                //使用cookies
                Cookie ck=new Cookie("PHPSESSID","78d8aj36nhtoauqcpnlrv33673");
               // requestScore.CookieContainer.Add(ck);
                //requestScore.CookieContainer."PHPSESSID=bia752fnm0681mr4jhnsmrde94; bdshare_firstime=1405997786686; Hm_lvt_9031c4bc2cecacb6d760cb393192453e=1405997787; Hm_lpvt_9031c4bc2cecacb6d760cb393192453e=1406005239; _ga=GA1.3.1567846960.1405997784";
                Stream stream = requestScore.GetRequestStream();
                stream.Write(data, 0, data.Length);
                stream.Close();
                HttpWebResponse responseSorce = (HttpWebResponse)requestScore.GetResponse();
                responseSorce.Cookies.Add(ck);
                StreamReader reader = new StreamReader(responseSorce.GetResponseStream(), Encoding.UTF8);
                return reader.ReadToEnd();
            }
        }
    }

    我要飞得更远!

    2014年7月22日 9:47
  • Hi X-H,

    你发在一般讨论来讨论这个问题可能不太合适。

    我不是很了解JSON所以我对具体的内容做了一些了解:

    1. JSON:http://baike.baidu.com/view/136475.htm?fr=aladdin

    2. 序列化和发序列化JSON:http://msdn.microsoft.com/en-us/library/bb412179%28v=vs.100%29.aspx

    3. 一些3方的JSON library:http://json.codeplex.com/

    首先你的问题可能就出在获取JSON上,那么或许你可以参照一下的页:

    http://stackoverflow.com/questions/5566942/how-to-get-a-json-string-from-url

    通过如下代码:

    var json = new WebClient().DownloadString("url");

    然后用一些类进行你所需要的转化,更详细的内容我建议你访问一下的论坛:

    ASP.NET中文:http://social.microsoft.com/Forums/zh-CN/home?forum=295

    C#中文:http://social.microsoft.com/Forums/zh-cn/home?forum=visualcshartzhchs

    WCF中文:http://social.microsoft.com/Forums/zh-CN/home?forum=wcfzhchs

    你用到的技术更我上面提到的论坛更相关一些。



    Barry
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年7月24日 5:49
    版主