Solved

C# get request to web service that returns json

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

Accepted Solution

by:
käµfm³d   👽 earned 250 total points
Comment Utility
"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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video teaches users how to migrate an existing Wordpress website to a new domain.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

772 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now