问题
I'm trying to port my WP8 application to a Windows Store App and I'm getting an HttpRequestException when performing the getStringAsync of the HttpClient.
Here is the code snippet giving me the issue:
HttpClient client = new HttpClient();
Stream stream = await client.GetStreamAsync(new Uri("http://www.psnapi.com.ar/ps3/api/psn.asmx/getPSNID?sPSNID=philippemexes", UriKind.Absolute));
and here is the complete exception message:
HttpRequestException: Response status code does not indicate success: 500 (Internal Server Error). I have to said that the indicated URL works in the browser and the exactly same code works properly in the Windows Phone project too.
Does anyone knows which could be a possible solution to get the desired xml to a string and why is this working for Windows Phone and not for a WinRT app?
Thank you very much Stefano
回答1:
Fiddler is your friend!
Appears that service isn't too happy about being called when there's not a user-agent string passed in the request headers. Try adding:
client.DefaultRequestHeaders.Add("user-agent", "blah blah blah");
between your two lines of code. There's nothing magic in the blah blah blah
part :), and it seems to work with your service, but who knows what, if any, interpretation the service is making of the UA string.
来源:https://stackoverflow.com/questions/16000863/winrt-c-sharp-httprequestexception-error-500