How do i get detailed info from a HttpWebResponse

爷,独闯天下 提交于 2019-12-22 00:36:22

问题


I'm making a post httpwebrequst, but in the HttpWebResponse im getting an error code 403 forbidden. Now that error isnt very use full to me.

I then tried a testprogram (that i do not have the source code to :() and used it to make the same post, and it returned with a code 403 forbidden but also told me tha SSL was needed. So is it possible to get more "serverside"details out of a failed httpwebrequest that just the errorcode?

thank you


Just to clear things up. Its fine that im getting the 403, i was just wondering why the test program could tell that SSL that SSL was needed when i can see anything like that in the webexception


回答1:


If a WebException is thrown because of a protocol error, its Response property will contain the actual response received from the web server.

try 
{
    // Do WebRequest
}
catch (WebException ex) 
{
    if (ex.Status == WebExceptionStatus.ProtocolError) 
    {
        HttpWebResponse response = ex.Response as HttpWebResponse;
        if (response != null)
        {
            // Process response
        }
    }
}



回答2:


There's not an explicit way to ask for more detailed information, no -- basically, ya get what ya get.

That said, Web servers often return documents along with those error codes that sometimes contain useful information, much like the one you got explaining the problem with the SSL cert. For help sniffing out problems like this, check out Fiddler -- it'll show you just about everything there is to know about your server responses.

As for your particular error, it's hard to say; 403 can indicate a few different things. But if you got back a response indicating something having to do with SSL, you may just be dealing with a bad or expired certificate (see this question), or the server may be requiring a secure connection but not getting one. Have you tried just hitting the URL directly with a Web browser, just to see whether you get prompted with a warning indicating a certificate problem, or anything other than an unmediated 403 response?



来源:https://stackoverflow.com/questions/516174/how-do-i-get-detailed-info-from-a-httpwebresponse

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