如何处理诸如501响应之类的execptions,并添加SOCKS支持? [英] How to handle execptions such as 501 response, and add SOCKS support?

查看:68
本文介绍了如何处理诸如501响应之类的execptions,并添加SOCKS支持?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用System.NET,我设法请求一个URL,获取HTTP响应代码和响应正文。



问题是每当服务器响应HTTP错误,我的程序将停止工作。



因此,如果用户意外地将SOCKS代理作为HTTP代理输入,有些人将以501响应错误,包括HTML中的响应正文,解释用户出了什么问题。如果用户尝试将代理用作带有
Web浏览器的HTTP代理,则会执行此操作。我的问题是,我无法弄清楚如何在异常的情况下捕获响应代码,以便我可以正确处理它。



代码我通常用来获取服务器响应:

I just started using System.NET and i managed to request a URL, fetch the HTTP response code and response body.

The problem is that whenever the server responds with a HTTP error, my program will stop working.

So if a user where to accidentally enter a SOCKS proxy as a HTTP proxy, some will respond with a 501 error, including a response body in HTML explaining what went wrong to the user. This is done if the user tries to use the proxy as a HTTP proxy with their web browser. My problem is, that i can't figure out how to catch the response code in case of an exception, so that i can handle it properly.

The code that i normally use to get the server response:

Dim response As WebResponse = request.GetResponse()

MsgBox(CType(response, HttpWebResponse).StatusCode)


我不知道为什么如果服务器给出501响应,这将不起作用,它确实适用于200 OK响应。



我发现我必须在try块中包含我的代码,然后在底部的某处包含以下内容:

I do not know why this will not work if the server gives a 501 response, it does work for 200 OK responses.

I found that i had to include my code in a try block, and then include the below somewhere at the bottom:

      Catch myerror As WebException
MsgBox(myerror.Message)






这将处理" ;错误"正确,并显示描述出错的错误消息。但是,我如何检查响应/错误代码编号,以向用户显示自定义错误?我可能只是检查消息,但我的印象是
这会根据用户系统上安装的语言而改变?



最后,有人能指出我关于SOCKS代理的一些资源,因为我真的想在我的程序中添加对SOCKS4和5以及HTTP的支持。我目前正在尝试添加对HTTP的支持,但可能会考虑如何在以后添加对SOCKS
代理的支持。






$


$





This will handle the "Error" correctly, and show an error message describing what went wrong. But how can i check the response/error code number, to display a custom error to the user? I could of cause just check the Message, but i have the impression that this changes depending on what language is installed on the users system?

Finally, can someone point me to some resources about SOCKS proxies, as i would really like to add support for SOCKS4 and 5 as well as HTTP in my program. I am currently just trying to add support for HTTP, but will likely look into how to add support for SOCKS proxies later on.








推荐答案

您好,

这里有一个示例,演示了如何处理webrequest中的HTTP错误代码。

here is a sample which demonstrates You how to handle HTTP error code in a webrequest.

HTTP错误不是例外。这些是协议的状态代码。

HTTP errors are no Exceptions. These are status codes from the protocol.

我希望这会有所帮助

对待Ellen

        Dim lRequest As Net.HttpWebRequest        Dim lResponse As Net.HttpWebResponse        Dim lUrl As New Uri(Me.TextBox1.Text)        Dim mProxy As IWebProxy = WebRequest.GetSystemWebProxy        lRequest = HttpWebRequest.Create(lUrl)        lRequest.Timeout = 10000        ' proxy :         If CheckBox1.Checked Then            mProxy.Credentials = CredentialCache.DefaultCredentials            lRequest.Proxy = mProxy        End If        lRequest.Method = "GET"        lRequest.UserAgent = "Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.1;+.NET+CLR+1.1.4322)"        Try            lResponse = lRequest.GetResponse()            If lResponse.StatusCode = Net.HttpStatusCode.OK Then                Dim lReader As IO.Stream = lResponse.GetResponseStream()                Dim lWriter As New IO.FileStream(lLocal, IO.FileMode.Create)                Dim len As Long = lResponse.ContentLength                Dim type As String = lResponse.ContentType                Dim lLength As Long                Dim progress As Long                             Dim lBytes(512) As Byte                Do                    lLength = lReader.Read(lBytes, 0, lBytes.Length)                    lWriter.Write(lBytes, 0, lLength)                    progress += lLength                    Me.ProgressBar1.Value = (progress * 100) / len                Loop While lLength > 0                lWriter.Close()                lReader.Close()            Else                MsgBox(String.Format("Server returned: {0} {1}", _                           lResponse.StatusCode, lResponse.StatusDescription))            End If        Catch ex As Exception            MsgBox(ex.Message, MsgBoxStyle.Critical)        End Try





这篇关于如何处理诸如501响应之类的execptions,并添加SOCKS支持?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆