Solved

C# get request to web service that returns json

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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 …
FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

777 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