WinRT C# - HttpRequestException: error 500

天大地大妈咪最大 提交于 2019-12-19 10:21:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!