【问题标题】:Read Json from url without download从 url 读取 Json 而无需下载
【发布时间】:2017-03-07 08:48:50
【问题描述】:

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

当我使用 C# WebBrowser 时,它要求我下载文件以阅读 我尝试使用 WebClient DownloadString,但该站点使用登录页面中的 cookie,并显示 arlert“请求无效”:

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

更新:这是我的代码:

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:我尝试获取/设置 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");

【问题讨论】:

  • 能否提供更多信息,例如您目前编写的代码等?
  • 考虑WebClient或HttpRequest?
  • @Thematkinson 我更新了我的代码。
  • @LeiYang 本站使用cookie显示json字符串,authen.dzogame.com:8080/ReturnLogin.ashx登录后才显示,如果我在Webbrowser登录并在WebClient获取json字符串将是“Request Invaild”
  • cookies 可以通过程序设置在WebClient 或HttpRequest

标签: c# json winforms webbrowser-control


【解决方案1】:

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

获取 httpOnly 有点棘手。

  1. 将此代码添加到您的课程中,以便您可以获取 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();
            }
        }
    }
    

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 我读过,但我想读Json字符串而不下载文件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-22
    • 2021-11-22
    • 1970-01-01
    • 2013-10-04
    • 2015-01-20
    相关资源
    最近更新 更多