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

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

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

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

在此处输入图像描述

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

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

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

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

更新:到目前为止,我的所有搜索都没有使用WebBrowser控件来获取JSON响应。 也许我接近这完全错了? 我错过了什么?

不要使用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(); } } } 

您可以通过API文章和此主题在此C#.NET通信中找到更多详细信息。

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

-UPDATE-

我刚刚找到了适用于我的解决方案。 有关详细信息,请参阅以下链接,但以防我在此处粘贴了答案。 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进行了测试。

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

 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,然后运行以修改您的注册表。