从 url 读取 Json 无需下载

Read Json from url without download

我想从 URL 读取 Json 字符串,但是当我使用 Chrome 网页时会显示这个字符串:

当我使用 C# WebBrowser 时,它要求我下载文件以阅读 我尝试使用 WebClient DownloadString 但此站点使用登录页面中的 cookie,它显示 arlert "Request Invaild":

请帮助我,如果我的英语不好,我很抱歉

更新: 这是我的代码:

wbTest.Navigate("http://authen.dzogame.com:8080/LauncherLogin.aspx?gid=200");
wbTest.Document.GetElementById("tbxUserName").SetAttribute("value", "7honda");
wbTest.Document.GetElementById("tbxPassword").SetAttribute("value", "111111");
wbTest.Document.GetElementById("btnLogin").InvokeMember("click");

更新 2: 我尝试 get/set cookie,但它不起作用。

wb.Navigate("http://authen.dzogame.com:8080/LauncherLogin.aspx?gid=200");
Wait();
id = txtID.Text;
pass = txtPassword.Text;
wb.Document.GetElementById("tbxUserName").SetAttribute("value", id);
wb.Document.GetElementById("tbxPassword").SetAttribute("value", pass);
wb.Document.GetElementById("btnLogin").InvokeMember("Click");
Wait();
string cookie = wb.Document.Cookie;
WebClient wc = new WebClient();
wc.Headers.Add(HttpRequestHeader.Cookie, cookie);
string token = wc.DownloadString("http://authen.dzogame.com:8080/ReturnLogin.ashx");

看看这个开源 JSON 框架:http://www.newtonsoft.com/json

dynamic responseObject = JsonConvert.DeserializeObject("{\"status\":\"1\"...");

您尝试通过浏览器控件发送带有 cookie 的查询,但 属性 cookie 不包含 httpOnly cookie => 因此您没有会话 cookie。

获取 httpOnly 有点棘手。

  1. 将此代码添加到您的 class 以便您可以获得 httpOnly cookie。

    [DllImport("wininet.dll", SetLastError = true)]
    public static extern bool InternetGetCookieEx(
    string url,
    string cookieName,
    StringBuilder cookieData,
    ref int size,
    Int32 dwFlags,
    IntPtr lpReserved);
    
    private const Int32 InternetCookieHttponly = 0x2000;
    
    /// <summary>
    /// Gets the URI cookie container.
    /// </summary>
    /// <param name="uri">The URI.</param>
    /// <returns></returns>
    public static string GetUriCookieContainer(Uri uri)
    {
        // Determine the size of the cookie
        int datasize = 8192 * 16;
        StringBuilder cookieData = new StringBuilder(datasize);
        if (!InternetGetCookieEx(uri.ToString(), null, cookieData, ref datasize, InternetCookieHttponly, IntPtr.Zero))
        {
            if (datasize < 0)
                return null;
            // Allocate stringbuilder large enough to hold the cookie
            cookieData = new StringBuilder(datasize);
            if (!InternetGetCookieEx(
                uri.ToString(),
                null, cookieData,
                ref datasize,
                InternetCookieHttponly,
                IntPtr.Zero))
                return null;
        }
        return cookieData.ToString();
    }
    
  2. 你可以通过下面的代码得到json

    private void func()
    {
        // your code for login here ....
    
    
    
    
        var urlJson = new Uri("http://authen.dzogame.com:8080/LauncherLogin.aspx?gid=200");
    
        var cookie = GetUriCookieContainer(urlJson);
    
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://authen.dzogame.com:8080/ReturnLogin.ashx");
        request.Headers.Add(HttpRequestHeader.Cookie, cookie);
        request.Accept = "*/*";
        string jsonResponse = null;
        using (WebResponse response = request.GetResponse())
        {
            using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
            {
                jsonResponse = streamReader.ReadToEnd();
            }
        }
    }