如何使用WebBrowser控件来捕获JSON响应 [英] How to capture JSON response using WebBrowser control

查看:3989
本文介绍了如何使用WebBrowser控件来捕获JSON响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发布使用 WebBrowser.Navigate()网站的JSON响应网址

一切顺利,包括 webBrowser1_DocumentCompleted()事件处理函数被调用。

All goes well, including the webBrowser1_DocumentCompleted() event handler being called.

而是获得一个安静的反应(如 webBrowser1.Document ),我可以以编程方式处理,我收到一个文件下载对话框:

But instead of getting a "quiet" response (e.g. webBrowser1.Document) that I can handle programmatically, I receive a File Download dialog box:

如果我点击保存按钮后检查该文件,它包含完全相同的JSON响应,我期望的那样。

If I click the Save button and later examine the file, it contains exactly the JSON response that I expect.

不过,我想程序捕获在这个JSON响应-code,而不显示该对话框,并具有点击保存按钮。

But I want the program capture this JSON response in-code, without displaying that dialog and having to click the Save button.

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

How do I capture JSON response using WebBrowser control?

注:的张贴这个问题,我搜索此之前,所有我发现了一个类似的问题为其接受的答案不真正解释如何做到这一点(我已经处理 webBrowser1_DocumentCompleted )。 ?任何提示

Note: before posting this question I searched SO and all I found was a similar question for which the accepted answer doesn't really explain how to do this (I'm already handling webBrowser1_DocumentCompleted). Any tips?

更新:所有我的搜索,到目前为止取得了什么关于使用WebBrowser控件来获取JSON响应。也许我处理这个完全错误的?我缺少什么?

Update: All my searches so far yielded nothing in regard to using WebBrowser control to fetch JSON responses. Perhaps I am approaching this completely wrong? What am I missing?

推荐答案

不要使用 web浏览器的JSON通讯。使用的WebRequest 来代替:

Don't use WebBrowser for JSON communication. Use WebRequest instead:

//
//    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通信的更多详细信息< A HREF =htt​​p://stackoverflow.com/q/5756147>此线程

You can find more details in this C# .NET communication with API article and this thread.

这篇关于如何使用WebBrowser控件来捕获JSON响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆