首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >带代理的VB.net HttpWebRequest

带代理的VB.net HttpWebRequest
EN

Stack Overflow用户
提问于 2013-07-29 03:22:24
回答 3查看 10K关注 0票数 1

当我不使用代理,但我想使用代理,以便在发送请求时不会暴露我的真实IP时,我的代码可以正常工作。每当我尝试运行我的程序时,它都会给我一个错误,说"The remote server returned an error:(417) error“。它指向"response = CType(request.GetResponse(),HttpWebResponse)“。我的代码有什么问题吗?我现在真的很困惑。任何帮助都能帮上忙。

代码语言:javascript
运行
复制
            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()
EN

回答 3

Stack Overflow用户

发布于 2016-04-13 06:04:40

代码语言:javascript
运行
复制
<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

结束类

票数 1
EN

Stack Overflow用户

发布于 2014-12-11 18:03:52

代码语言:javascript
运行
复制
 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()
票数 0
EN

Stack Overflow用户

发布于 2016-12-08 03:05:26

Cahnge的台词:

代码语言:javascript
运行
复制
Dim myProxy As New WebProxy("173.234.249.68", 8800)

代码语言:javascript
运行
复制
Dim myProxy As New WebProxy("173.234.249.68:8800")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17911946

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档