Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture JSON response using WebBrowser control

I POST to website's JSON-response URL using WebBrowser.Navigate().

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

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

enter image description here

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

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

How do I capture JSON response using WebBrowser control?

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?

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?

like image 746
scatmoi Avatar asked Oct 22 '12 23:10

scatmoi


3 Answers

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\",\"[email protected]\"],[\"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();


        }
    }
}

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

like image 96
Not So Sharp Avatar answered Nov 10 '22 17:11

Not So Sharp


I have the same problem as Scatmoi but I can't use a web request due to login requirements. I tried to modify the answer above to see if I could get the login authentication to pass but no luck.

-UPDATE-

I just found the solution that will work for me. See the following link for more info but just in case I have pasted the answer here. http://www.codeproject.com/Tips/216175/View-JSON-in-Internet-Explorer

Need to view JSON responses in IE? 1.Open Notepad and paste the following:

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.Save document as IE-Json.reg and then run it.

Note: This has been tested on Windows XP and Windows 7 using IE 7, 8, 9, 10.

like image 5
gadildafissh Avatar answered Nov 10 '22 17:11

gadildafissh


Above solution was missing two things, and below code should work in every situation:

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

Just save it file json.reg, and run to modify your registry.

like image 5
Tomasz Maj Avatar answered Nov 10 '22 17:11

Tomasz Maj