我正在尝试使用HTTPWebRequets代理,它可以正常工作,直到我尝试发布数据。它出于某种原因保持超时。附件是我用于webrequests的类。有人可以提供建议。
Imports System.IO
Imports System.Net
Public Class EasyHttp Public Enum HTTPMethod As Short HTTP_GET = 0 HTTP_POST = 1 结束枚举 公共代理作为字符串 公共端口为整数 Public UseProxy As Integer = 0 Public Sub New() '没有Args构造函数 结束子
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
结束班
答案 0 :(得分:0)
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