2013-07-28 96 views
0

我的代碼工作時,我不使用代理,但我想使用代理,以便我發送請求時不會顯示我的真實IP。每當我嘗試運行我的程序時,它都會給我一個錯誤,說「遠程服務器返回錯誤:(417)期望失敗。」它指向「response = CType(request.GetResponse(),HttpWebResponse)」。我的代碼有問題嗎?我現在很困惑。任何幫助都會感謝。VB.net HttpWebRequest與代理

  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() 
+0

是有什麼錯我的代理報關? –

回答

0
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() 
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 

末級

+0

你能解釋你的代碼是如何工作的?這將使OP和其他人能夠在其他地方瞭解和應用您的方法(如果適用)。僅有代碼的答案[不鼓勵](http://meta.stackexchange.com/q/148272/284827),可能會被刪除。 - [評論期間](http://stackoverflow.com/review/late-answers/11994866) –

0

Cahnge行:

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

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