使用 HTTPWebRequest 和 Post 的代理

发布于 2024-10-17 08:58:11 字数 2042 浏览 1 评论 0原文

我正在尝试将代理与 HTTPWebRequets 一起使用,并且在我尝试发布数据之前它工作得很好。由于某种原因它总是超时。附件是我用于网络请求的类。有人可以请建议吗?

Imports System.IO

导入 System.Net

公共类 EasyHttp 公共枚举 HTTPMethod 尽可能短 HTTP_GET = 0 HTTP_POST = 1 结束枚举 公共代理作为字符串 公共端口作为整数 公共 UseProxy 作为整数 = 0 公共子新建() '无参数构造函数 结束子

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 = New 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

结束类

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 = New 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九厘米的零° 2024-10-24 08:58:11
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
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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文