16

我使用 POST 到网站的 JSON 响应 URL WebBrowser.Navigate()

一切顺利,包括webBrowser1_DocumentCompleted()被调用的事件处理程序。

但是webBrowser1.Document,我没有得到一个我可以以编程方式处理的“安静”响应(例如),而是收到一个文件下载对话框:

在此处输入图像描述

如果我单击该Save按钮并稍后检查该文件,它将完全包含我期望的 JSON 响应。

但我希望程序在代码中捕获这个 JSON 响应,而不显示该对话框并且必须单击Save按钮。

如何使用 WebBrowser 控件捕获 JSON 响应?

注意:在发布这个问题之前,我搜索了 SO,我发现的只是一个类似的问题,接受的答案并没有真正解释如何做到这一点(我已经在处理webBrowser1_DocumentCompleted)。有小费吗?

更新:到目前为止,我的所有搜索都没有产生关于使用 WebBrowser 控件获取 JSON 响应的任何信息。也许我正在接近这个完全错误的?我错过了什么?

4

4 回答 4

11

不要WebBrowser用于 JSON 通信。改用WebRequest

//
//    EXAMPLE OF LOGIN REQUEST 
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a request using a URL that can receive a post. 
            WebRequest request = WebRequest.Create("http://getting-started.postaffiliatepro.com/scripts/server.php");
            // Set the Method property of the request to POST.
            request.Method = "POST";
            // Create POST data and convert it to a byte array.
            //WRITE JSON DATA TO VARIABLE D
            string postData = "D={\"requests\":[{\"C\":\"Gpf_Auth_Service\", \"M\":\"authenticate\", \"fields\":[[\"name\",\"value\"],[\"Id\",\"\"],[\"username\",\"user@example.com\"],[\"password\",\"ab9ce908\"],[\"rememberMe\",\"Y\"],[\"language\",\"en-US\"],[\"roleType\",\"M\"]]}],  \"C\":\"Gpf_Rpc_Server\", \"M\":\"run\"}";
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            // Set the ContentType property of the WebRequest.
            request.ContentType = "application/x-www-form-urlencoded";
            // Set the ContentLength property of the WebRequest.
            request.ContentLength = byteArray.Length;
            // Get the request stream.
            Stream dataStream = request.GetRequestStream();
            // Write the data to the request stream.
            dataStream.Write(byteArray, 0, byteArray.Length);
            // Close the Stream object.
            dataStream.Close();
            // Get the response.
            WebResponse response = request.GetResponse();
            // Display the status.
//            Console.WriteLine(((HttpWebResponse)response).StatusDescription);
            // Get the stream containing content returned by the server.
            dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd();
            // Display the content.
            Console.WriteLine(responseFromServer);
            // Clean up the streams.
            reader.Close();
            dataStream.Close();
            response.Close();


        }
    }
}

您可以在此C# .NET 与 API 通信文章此线程中找到更多详细信息。

于 2012-10-23T12:03:27.510 回答
5

我和 Scatmoi 有同样的问题,但由于登录要求,我无法使用 Web 请求。我试图修改上面的答案,看看我是否可以通过登录身份验证,但没有运气。

-更新-

我刚刚找到了适合我的解决方案。有关更多信息,请参阅以下链接,但以防万一我在此处粘贴了答案。http://www.codeproject.com/Tips/216175/View-JSON-in-Internet-Explorer

需要在 IE 中查看 JSON 响应?1.打开记事本并粘贴以下内容:

Windows Registry Editor Version 5.00;
; Tell IE 7,8,9,10 to open JSON documents in the browser on Windows XP and later.
; 25336920-03F9-11cf-8FD0-00AA00686F13 is the CLSID for the "Browse in place" .
;
[HKEY_CLASSES_ROOT\MIME\Database\Content Type\application/json]
"CLSID"="{25336920-03F9-11cf-8FD0-00AA00686F13}"
"Encoding"=hex:08,00,00,00

2.将文档另存为IE-Json.reg,然后运行。

注意:这已经在 Windows XP 和 Windows 7 上使用 IE 7、8、9、10 进行了测试。

于 2013-08-12T13:36:34.887 回答
5

上面的解决方案缺少两件事,下面的代码应该适用于各种情况:

Windows Registry Editor Version 5.00
;
; Tell IE to open JSON documents in the browser.  
; 25336920-03F9-11cf-8FD0-00AA00686F13 is the CLSID for the "Browse in place" .
;  

[HKEY_CLASSES_ROOT\MIME\Database\Content Type\application/json]
"CLSID"="{25336920-03F9-11cf-8FD0-00AA00686F13}"
"Encoding"=hex:08,00,00,00

[HKEY_CLASSES_ROOT\MIME\Database\Content Type\application/x-json]
"CLSID"="{25336920-03F9-11cf-8FD0-00AA00686F13}"
"Encoding"=hex:08,00,00,00

[HKEY_CLASSES_ROOT\MIME\Database\Content Type\text/json]
"CLSID"="{25336920-03F9-11cf-8FD0-00AA00686F13}"
"Encoding"=hex:08,00,00,00

只需将其保存为 json.reg 文件,然后运行以修改您的注册表。

于 2014-04-07T05:45:20.563 回答
0

作为对 gadildafissh 和 Tomasz Maj 的 awnser 的扩展,这可以通过编程方式完成。只有一个缺点,这必须以管理员权限完成。我的示例是针对 64 位设备上的 32 位应用程序。

   private bool SetRegistery()
    {
        try
        {
            using (var hklm = RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Registry64))
            {
                using (RegistryKey key = hklm.OpenSubKey(@"MIME\Database\Content Type\application/json", true))
                {
                    if (key != null)
                    {
                        key.SetValue("CLSID", "{25336920-03F9-11cf-8FD0-00AA00686F13}");
                        key.SetValue("Encoding", new byte[] { 0x80, 0x00, 0x00, 0x00 });
                    }
                }
            }
            return true;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        return false;
    }
于 2019-05-02T15:40:45.207 回答