Solved

C# get request to web service that returns json

Posted on 2014-03-07
3
7,157 Views
Last Modified: 2014-03-12
When I enter the URL manually in IE, I get the json response I expect. However, when I do the following code below, I get this error:

Additional information: The remote server returned an error: (500) Internal Server Error.

string sURL = "http://localhost:56925/Get/GetVMServerInfo?hostName=" + host + "&userName=" + username + "&password=" + password + "&serverName=";

WebRequest request = WebRequest.Create(sURL);
request.Method = "GET";
request.ContentType = "application/json; charset=utf-8";

var response = (HttpWebResponse)request.GetResponse();
string jsonText;
using (var sr = new StreamReader(response.GetResponseStream())) {
   jsonText = sr.ReadToEnd();
}

Open in new window

Error happens at var response = (HttpWebResponse)request.GetResponse(); line 7.

The same with this code:
var jsonText = new WebClient().DownloadString(sURL);
dynamic allServers = JsonConvert.DeserializeObject(jsonText);

Open in new window

Error happens at var jsonText = new WebClient().DownloadString(sURL); line 1.
0
Comment
Question by:itnifl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 250 total points
ID: 39912998
"Internal Server Error" means that an exception happened in your code on the server--or in the worst case something is wrong with IIS. You need to check that your code isn't throwing any exceptions.

Also, if the service returns JSON, and that's what you want back, then you need to be setting the Accept header, not the Content-Type header. Content-Type is what you are sending; Accept is what you want back. GET requests don't have a content body, so assigning a Content-Type doesn't make sense.
0
 
LVL 15

Assisted Solution

by:Minh Võ Công
Minh Võ Công earned 250 total points
ID: 39914173
Do you try ContentType="application/x-www-form-urlencoded"
http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.contenttype(v=vs.110).aspx
// Set the 'ContentType' property of the WebRequest.
			myWebRequest.ContentType="application/x-www-form-urlencoded";

			// Set the 'ContentLength' property of the WebRequest.
			myWebRequest.ContentLength=byteArray.Length;
			Stream newStream=myWebRequest.GetRequestStream();
			newStream.Write(byteArray,0,byteArray.Length);

			// Close the Stream object.
			newStream.Close();

			// Assign the response object of 'WebRequest' to a 'WebResponse' variable.
			WebResponse myWebResponse=myWebRequest.GetResponse();

Open in new window

0
 
LVL 2

Author Closing Comment

by:itnifl
ID: 39923589
The real problem was that the request was not getting parsed correctly by the web server. But also I used the information both of you gave or linked to, to end up with a correct code.

            try {
               HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sURL);
               request.Method = "GET";
               request.Accept = "application/json; charset=utf-8";
               var response = (HttpWebResponse)request.GetResponse();

               using (var sr = new StreamReader(response.GetResponseStream())) {
                  jsonText = sr.ReadToEnd();
               }
            } catch (Exception e) {
               throw e;
            }
0

Featured Post

Report: Liquid Web beats Amazon, Rackspace & More

A study by performance analyst firm Cloud Spectator finds that Liquid Web beats rivals Amazon, Rackspace and DigitalOcean when it comes to website and cloud application performance.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question