最近在用C#調用Java寫的WebService時,發現老是返回500 服務器錯誤,到底什么原因一直找不出來,
后來google了以后,找到國外的http://stackoverflow.com站點已經有人碰到過這個問題了。
轉帖如下:
HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest; WebResponse wr = req.GetResponse();
When the server returns 500 Internal Server Error, exception is thrown in req.GetResponse(). I would like the GetResponse() to accept this Response Code, it is normal for the passed url to throw this Response Code. I would like to parse the Html despite Response Code 500 Internal Server Error. Is it possible to say to GetResponse() method not to verify the Response Code?
Answer:
try { HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest; WebResponse wr = req.GetResponse(); } catch (WebException wex) { var pageContent = new StreamReader(wex.Response.GetResponseStream()) .ReadToEnd(); }
可以通過上面的代碼查詢具體的錯誤信息,再進一步解決問題。
原帖地址:
http://stackoverflow.com/questions/18403846/httpwebrequest-accept-500-internal-server-error