?
Solved

C# get request to web service that returns json

Posted on 2014-03-07
3
Medium Priority
?
7,456 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 750 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 750 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
The viewer will learn how to dynamically set the form action using jQuery.
Introduction to Processes
Suggested Courses
Course of the Month8 days, 2 hours left to enroll

765 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