从url读取Json而不下载

时间:2017-03-07 08:48:50

标签: c# json winforms webbrowser-control

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

enter image description here

当我使用C#WebBrowser时,它会让我下载文件来阅读 我尝试使用WebClient DownloadString,但是这个站点使用来自登录页面的cookie,它显示了arlert" Request Invaild":

enter image description here

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

更新:这是我的代码:

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/locale/pt.js"></script>

更新2:我尝试获取/设置Cookie,但它无效。

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 个答案:

答案 0 :(得分:1)

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

// Removes default background.
mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
myPopupWindow.setOutsideTouchable(true);

答案 1 :(得分:1)

您尝试使用浏览器控件中的Cookie发送查询,但属性Cookie不包含httpOnly cookie =&gt;所以你没有会话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();
            }
        }
    }