sca*_*moi 15 c# json webbrowser-control
我使用POST发布到网站的JSON响应URL WebBrowser.Navigate().
一切顺利,包括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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
您可以通过API文章和此主题在此C#.NET通信中找到更多详细信息.
我和 Scatmoi 有同样的问题,但由于登录要求,我无法使用网络请求。我试图修改上面的答案,看看我是否可以通过登录身份验证,但没有运气。
-更新-
我刚刚找到了适合我的解决方案。请参阅以下链接以获取更多信息,但以防万一我在此处粘贴了答案。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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
只需将其保存为 json.reg 文件,然后运行以修改您的注册表。
| 归档时间: |
|
| 查看次数: |
14854 次 |
| 最近记录: |