开发者

Proxies With HTTPWebRequest and Post

开发者 https://www.devze.com 2023-02-10 14:25 出处:网络
I\'m trying to use proxies with HTTPWebRequets and it works just fine until I try and post data. It keeps timing out for some reason. Attached is the class I use for webrequests. Can someone please ad

I'm trying to use proxies with HTTPWebRequets and it works just fine until I try and post data. It keeps timing out for some reason. Attached is the class I use for webrequests. Can someone please advise.

Imports System.IO

Imports System.Net

Public Class EasyHttp Public Enum HTTPMethod As Short HTTP_GET = 0 HTTP_POST = 1 End Enum Public Proxy As String Public Port As Integer Public UseProxy As Integer = 0 Public Sub New() 'No Args Constructor End Sub

Public Function Send(ByVal URL As String, _
    Optional ByVal PostData As String = "", _
    Optional ByVal Method As String = "", _
    Optional ByVal ContentType As String = "")


    Dim request As HttpWebRequest
    request = WebRequest.Create(URL)

    request.ServicePoint.Expect100Continue = False

    Dim Response As HttpWebResponse

    Dim SW As StreamWriter
    Dim SR As StreamReader
    Dim ResponseData As String

    If UseProxy = 1 Then
        request.Proxy = New WebProxy(Proxy, Port)

    End If

    ' Prepare Request Object
    request.Method = Method

    ' Set form/post content-type if necessary
    If (Method = "POST" AndAlso PostData <> "" AndAlso ContentType = "") Then
        ContentType = "application/x-www-form-urlencoded"
    End If

    ' Set Content-Type
    If (ContentType <> "") Then
        request.ContentType = ContentType
        request.ContentLength = PostData.Length
    End If

    ' Send Request, If Request
    If (Method = "POST") Then
        ' Try
        SW = N开发者_如何转开发ew StreamWriter(request.GetRequestStream())
        SW.Write(PostData)
        ' Catch Ex As Exception
        'Throw Ex
        ' Finally
        'SW.Close()
        '  End Try
    End If

    ' Receive Response
    '  Try
    Response = request.GetResponse()
    SR = New StreamReader(Response.GetResponseStream())
    ResponseData = SR.ReadToEnd()
    '  Catch Wex As System.Net.WebException
    '    SR = New StreamReader(Wex.Response.GetResponseStream())
    '     ResponseData = SR.ReadToEnd()
    '     Throw New Exception(ResponseData)
    '  Finally
    '     SR.Close()
    '  End Try

    Return ResponseData
End Function

End Class


Imports System.Text

Imports System.Net

Function Post(ByVal Site As String, ByVal Data As String, ByVal Proxy As String, ByVal UserAgent As String)
    Dim responseData As String = ""
    Try
        Dim request As Net.HttpWebRequest = Net.WebRequest.Create(Site)
        Dim myProxy As New WebProxy(Proxy)
        request.Proxy = myProxy
        request.Accept = "*/*"
        request.AllowAutoRedirect = True
        request.UserAgent = UserAgent
        request.Timeout = 60000
        request.Method = "POST"
        If request.Method = "POST" Then
            request.ContentType = "application/x-www-form-urlencoded"
            Dim encoding As New ASCIIEncoding()
            Dim postByteArray() As Byte = encoding.GetBytes(Data)
            request.ContentLength = postByteArray.Length
            Dim postStream As IO.Stream = request.GetRequestStream()
            postStream.Write(postByteArray, 0, postByteArray.Length)
            postStream.Close()
        End If
        Dim response As Net.HttpWebResponse = request.GetResponse()
        If response.StatusCode = Net.HttpStatusCode.OK Then
            Dim responseStream As IO.StreamReader = _
              New IO.StreamReader(response.GetResponseStream())
            responseData = responseStream.ReadToEnd()
        End If
        response.Close()
    Catch e As Exception
        responseData = "An error occurred: " & e.Message
    End Try
    Post = responseData
End Function
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号