【问题标题】:VB.net HttpWebRequest with Proxy带有代理的 VB.net HttpWebRequest
【发布时间】:2024-04-26 14:25:01
【问题描述】:

我的代码在我不使用代理但我想使用代理时有效,以便在发送请求时不会显示我的真实 IP。每当我尝试运行我的程序时,它都会给我一个错误提示“远程服务器返回错误:(417)预期失败。”它指向“response = CType(request.GetResponse(), HttpWebResponse)”。我的代码有问题吗?我现在真的很困惑。任何帮助都会非常感谢。

            Dim myProxy As New WebProxy("173.234.249.68", 8800)
            Dim request As HttpWebRequest
            Dim response As HttpWebResponse
            Dim tempCookies As New CookieContainer
            request = CType(WebRequest.Create("http://samplewebsite.com"), HttpWebRequest)
            request.Proxy = myProxy
            request.ContentType = "application/x-www-form-urlencoded"
            request.ContentLength = POST.Length
            request.KeepAlive = True
            request.CookieContainer = tempCookies

            response = CType(request.GetResponse(), HttpWebResponse)
            tempCookies.Add(response.Cookies)
            response.Close()

【问题讨论】:

  • 我的代理声明有问题吗?

标签: vb.net proxy httpwebrequest httpwebresponse


【解决方案1】:
<Runtime.InteropServices.DllImport("wininet.dll", SetLastError:=True)> _
Private Shared Function InternetSetOption(ByVal hInternet As IntPtr, ByVal dwOption As Integer, ByVal lpBuffer As IntPtr, ByVal lpdwBufferLength As Integer) As Boolean
End Function


Public Structure Struct_INTERNET_PROXY_INFO
    Public dwAccessType As Integer
    Public proxy As IntPtr
    Public proxyBypass As IntPtr
End Structure

Private Sub UseProxy(ByVal strProxy As String)
    Const INTERNET_OPTION_PROXY As Integer = 38
    Const INTERNET_OPEN_TYPE_PROXY As Integer = 3

    Dim struct_IPI As Struct_INTERNET_PROXY_INFO

    struct_IPI.dwAccessType = INTERNET_OPEN_TYPE_PROXY
    struct_IPI.proxy = Marshal.StringToHGlobalAnsi(strProxy)
    struct_IPI.proxyBypass = Marshal.StringToHGlobalAnsi("local")

    Dim intptrStruct As IntPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(struct_IPI))

    Marshal.StructureToPtr(struct_IPI, intptrStruct, True)

    Dim iReturn As Boolean = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY, intptrStruct, System.Runtime.InteropServices.Marshal.SizeOf(struct_IPI))
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Label4.Text = (TextBox1.Text & ":" & TextBox2.Text)

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    UseProxy(Label4.Text)
    WebBrowser1.Navigate(TextBox3.Text)
End Sub

结束类

【讨论】:

  • 你能解释一下你的代码是如何/为什么工作的吗?这将使 OP 和其他人能够理解并在其他地方应用您的方法(如果适用)。纯代码答案是discouraged,可能会被删除。 — During review
【解决方案2】:
 Dim myProxy As New WebProxy("173.234.249.68:8800", true)
        Dim request As HttpWebRequest
        Dim response As HttpWebResponse
        Dim tempCookies As New CookieContainer
        request = CType(WebRequest.Create("http://samplewebsite.com"), HttpWebRequest)
        request.Proxy = myProxy
        request.ContentType = "application/x-www-form-urlencoded"
        request.ContentLength = POST.Length
        request.KeepAlive = True
        request.CookieContainer = tempCookies

        response = CType(request.GetResponse(), HttpWebResponse)
        tempCookies.Add(response.Cookies)
        response.Close()

【讨论】:

    【解决方案3】:

    换行:

    Dim myProxy As New WebProxy("173.234.249.68", 8800)
    

    Dim myProxy As New WebProxy("173.234.249.68:8800")
    

    【讨论】:

      最近更新 更多