使用 HTTPWebRequest 和 Post 的代理
我正在尝试将代理与 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)