Click to See Complete Forum and Search --> : Convert Incoming JSON to String


compbrat75
07-07-2011, 02:13 PM
I'm running in circles.
I'm using ASP.NET 2.0 VB.NET...

All I want to do is take an incoming JSON and send it to Response.Write() so it displays as a text string.

I don't want to parse it or deserialize it. The string is being passed to a mainframe and it's expected a text string.

I keep getting a 400 Bad Request.

Here's the latest code snippet...

<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="utf-8" aspcompat="true" Debug="true" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace= "System.Web.Extensions" %>
<%@ Import Namespace="System.Web.Script.Serialization" %>


<%
Dim pgRequest As System.Net.WebRequest
Dim pgResponse As System.Net.WebResponse
Dim buffer() As Byte
Dim iBytesToRead As Integer
Dim iBytesRead As Integer
Dim n As Integer
Dim t As System.IO.Stream

Dim URL = Request.QueryString("url")
pgRequest = System.Net.WebRequest.Create(URL)
pgResponse = pgRequest.GetResponse()


Dim pgStream As StreamReader = new StreamReader(pgResponse.GetResponseStream())
Dim pgString As String

t = pgResponse.GetResponseStream()

iBytesToRead = pgResponse.ContentLength
ReDim buffer(iBytesToRead)
iBytesRead = 0

While iBytesToRead > 0
n = t.Read(buffer, iBytesRead, iBytesToRead)
If n = 0 Then
Exit While
End If
iBytesRead += n
iBytesToRead -= n
End While
Response.Write(buffer)
t.Close()
pgResponse.Close()

%>

Here's the error:

The remote server returned an error: (400) Bad Request.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Net.WebException: The remote server returned an error: (400) Bad Request.

Source Error:
Line 21: pgResponse = pgRequest.GetResponse()


Any help would be appreciated!

Ribeyed
07-08-2011, 07:55 AM
Hi,

Firstly you should wrap your code in a Try and Catch block to expose the error that occuring instead of letting it fall over and return a none descriptive error code.


Try
//your code here........
Catch ex As Exception
response.write ex.Message.ToString()
End Try



That will help you to debug your code better.

regads

Ribs

compbrat75
07-08-2011, 10:28 AM
Hi Ribs,
Thank you for the reply. Sorry, I had taken that code out so I could see the stack trace.
The error message is: The remote server returned an error: (400) Bad Request.
I've listed all of the various Exception properties and methods at different times and it doesn't provide me with any more helpful information.

I know that a direct call to the URL in Chrome returns the JSON, but not other browsers since Chrome has a built-in JSON parser.

What's tripping me up is how to get the Response so I can parse it.
I'm thinking this is a Header issue, but I'm new to JSON with VB.NET.

Any thoughts?
Thank you for replying!
~Maria

compbrat75
07-08-2011, 05:32 PM
After a lot of searching and some eventual help from the site returning the 400 Bad Request, this is solved.

If a server returns a Response Code that stops you from accessing JSON that is sent along as part of the response, you can access it from the Catch Block.

Here is the code that eventually worked for me:


Dim url As String = "url returning 400 or 500 codes"

Dim req As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
Try
Using response = req.GetResponse()
'' success statements
End Using
Catch ex As WebException
Console.WriteLine(ex.Status)
If ex.Response IsNot Nothing Then
'' can use ex.Response.Status, .StatusDescription
If ex.Response.ContentLength <> 0 Then
Using stream As Stream = ex.Response.GetResponseStream()
Using reader As StreamReader = New StreamReader(stream)
Response.Write(reader.ReadToEnd())
End Using
End Using
End If
End If
End Try



I hope that this helps someone!