代理错误:System.Net.ProtocolViolationException [英] Proxy Error: System.Net.ProtocolViolationException
问题描述
我收到错误:System.Net.ProtocolViolationException:当我尝试使用proxy for httpwebrequest时,必须在调用[Begin] GetResponse之前将ContentLength字节写入请求流。在使用代理之前我工作得很好。
Dim req As HttpWebRequest = DirectCast(WebRequest.Create(URL),HttpWebRequest)
'接下来3添加了行,然后遇到错误。
Dim myProxy As New WebProxy(ProxyURL)
myProxy.Credentials = New NetworkCredential(ProxyID,ProxyPwd)
req.Proxy = myProxy
req.KeepAlive = KeepAlive
req.AllowAutoRedirect = AllowAutoRedirect
req.Timeout = requestConnectTimeoutInMs
req.ReadWriteTimeout = requestReadWriteTimeoutInMs
'postData
Dim b As Byte()= System.Text.Encoding.UTF8.GetBytes(postData)
'POST
req.Method =" POST"
req.UserAgent =" Mozilla / 4.0(兼容; MSIE 7.0; Windows NT 6.1; Trident / 4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4 .0E)"
re q.ContentType =" application / x-www-form-urlencoded"
req.Headers.Add(" Cache-Control"," no-cache")
req.Headers.Add(" UA-CPU"," x86")
req.Headers.Add(HttpRequestHeader.AcceptLanguage," en -US")
req.Headers.Add(HttpRequestHeader.AcceptEncoding," gzip")
req.Headers.Add(HttpRequestHeader.AcceptEncoding ,"deflate")
'Referer
req.Referer = Referer
'发送Cookie
req.Headers.Add(" Cookie",Cookies)
'发送表单
req .ContentLength = b.Length
req.GetRequestStream.Write(b,0,b.Length)
Dim res As HttpWebResponse = DirectCast(req) .GetResponse(),HttpWebResponse)
返回资金
我遇到以下异常:
System.Net.ProtocolViolationException:在
调用[Begin] GetResponse之前,必须将ContentLength字节写入请求流。
在System.Net.HttpWebRequest.GetResponse()
在Proxy.Form1.Form1_Load(对象发件人,EventArgs e)
以下代码在没有代理的情况下工作。我曾尝试过询问但无济于事。可以
有人可以解决问题吗?
Dim stream As Stream = Nothing
Dim Str As String = String.Empty
Dim policy As HttpRequestCachePolicy = New HttpRequestCachePolicy( HttpRequestCacheLevel.Default)
HttpWebRequest.DefaultCachePolicy = policy
Dim url As String =" http://www.jj919.com/post.asp"
Dim req As HttpWebRequest = DirectCast(WebRequest.Create(URL),HttpWebRequest)
Dim noCachePolicy As HttpRequestCachePolicy = New HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore)
req。 CachePolicy = noCachePolicy
req.KeepAlive = True
req.AllowAutoRedirect = True
req.Timeout = 3000
req.ReadWriteTimeout = 3000
Dim p As新的WebProxy()
Dim pUri As New Uri(" http://202.75.43.20:808&)\"b $ b p.Address = pUri
p.Credentials = New NetworkCredential(" test&qu ot;," test")
req.Proxy = p
Dim postData As String = String.Format(" name = {0}"," sam")
Dim b As Byte()= System.Text.Encoding.UTF8.GetBytes(postData)
req.Method =" POST"
req.UserAgent =" Mozilla / 4.0(兼容; MSIE 7.0; Windows NT 6.1; Trident / 4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)"
req.ContentType =" application / x-www-form-urlencoded"
req.Headers.Add(" Cache-Control"," no-cache")
req.Headers.Add(" UA-CPU"," x86")
req.Headers.Add(HttpRequestHeader.AcceptLanguage," en-US")
req.Headers.Add(HttpRequestHeader.AcceptEncoding," gzip")
req.Headers.Add(HttpRequestHeader.AcceptEncoding, " deflate")
req.ContentLength = b.Length
尝试
req.GetRequestStream.Write(b,0,b.Length)
使用res As HttpWebResponse = DirectCast(req.GetResponse(),HttpWebResponse)
If(not res.Headers.Get(" Content-Encoding")Is Nothing)then
stream = New GZipStream(res.GetResponseStream,CompressionMode.Decompress)
Else
stream = res.GetResponseStream()
End if
使用sr As StreamReader = New System.IO.StreamReader(stream,Encoding.Default)
Console.WriteLine(sr.ReadToEnd)
End using
End using
Catch wex As Exception
Console.WriteLine(wex.T oString)
结束尝试
I received the error :System.Net.ProtocolViolationException: You must write ContentLength bytes to the request stream before calling [Begin]GetResponse when i try to use proxy for httpwebrequest. Everything i working fine before using proxy.
Dim req As HttpWebRequest = DirectCast(WebRequest.Create(URL), HttpWebRequest)
' The next 3 lines was added, after then encountered error.
Dim myProxy As New WebProxy(ProxyURL)
myProxy.Credentials = New NetworkCredential(ProxyID, ProxyPwd)
req.Proxy = myProxy
req.KeepAlive = KeepAlive
req.AllowAutoRedirect = AllowAutoRedirect
req.Timeout = requestConnectTimeoutInMs
req.ReadWriteTimeout = requestReadWriteTimeoutInMs
'postData
Dim b As Byte() = System.Text.Encoding.UTF8.GetBytes(postData)
'POST
req.Method = "POST"
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)"
req.ContentType = "application/x-www-form-urlencoded"
req.Headers.Add("Cache-Control", "no-cache")
req.Headers.Add("UA-CPU", "x86")
req.Headers.Add(HttpRequestHeader.AcceptLanguage, "en-US")
req.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip")
req.Headers.Add(HttpRequestHeader.AcceptEncoding, "deflate")
'Referer
req.Referer = Referer
'Send Cookies
req.Headers.Add("Cookie", Cookies)
'send the form
req.ContentLength = b.Length
req.GetRequestStream.Write(b, 0, b.Length)
Dim res As HttpWebResponse = DirectCast(req.GetResponse(), HttpWebResponse)
Return res
I encountered the following exception:
System.Net.ProtocolViolationException: You must write ContentLength bytes to the request stream before calling [Begin]GetResponse.
at System.Net.HttpWebRequest.GetResponse()
at Proxy.Form1.Form1_Load(Object sender, EventArgs e) in
The following code works without proxy. I had tried asking around but to no avail. Can somebody shed a light?
Dim stream As Stream = Nothing Dim Str As String = String.Empty Dim policy As HttpRequestCachePolicy = New HttpRequestCachePolicy(HttpRequestCacheLevel.Default) HttpWebRequest.DefaultCachePolicy = policy Dim url As String = "http://www.jj919.com/post.asp" Dim req As HttpWebRequest = DirectCast(WebRequest.Create(URL), HttpWebRequest) Dim noCachePolicy As HttpRequestCachePolicy = New HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore) req.CachePolicy = noCachePolicy req.KeepAlive = True req.AllowAutoRedirect = True req.Timeout = 3000 req.ReadWriteTimeout = 3000 Dim p As New WebProxy() Dim pUri As New Uri("http://202.75.43.20:808") p.Address = pUri p.Credentials = New NetworkCredential("test", "test") req.Proxy = p Dim postData As String = String.Format("name={0}", "sam") Dim b As Byte() = System.Text.Encoding.UTF8.GetBytes(postData) req.Method = "POST" req.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)" req.ContentType = "application/x-www-form-urlencoded" req.Headers.Add("Cache-Control", "no-cache") req.Headers.Add("UA-CPU", "x86") req.Headers.Add(HttpRequestHeader.AcceptLanguage, "en-US") req.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip") req.Headers.Add(HttpRequestHeader.AcceptEncoding, "deflate") req.ContentLength = b.Length Try req.GetRequestStream.Write(b, 0, b.Length) Using res As HttpWebResponse = DirectCast(req.GetResponse(), HttpWebResponse) If (Not res.Headers.Get("Content-Encoding") Is Nothing) Then stream = New GZipStream(res.GetResponseStream, CompressionMode.Decompress) Else stream = res.GetResponseStream() End If Using sr As StreamReader = New System.IO.StreamReader(stream, Encoding.Default) Console.WriteLine(sr.ReadToEnd) End Using End Using Catch wex As Exception Console.WriteLine(wex.ToString) End Try
这篇关于代理错误:System.Net.ProtocolViolationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!