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
Solved

C# get request to web service that returns json

Posted on 2014-03-07
3
7,039 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
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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

808 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