本文介绍了代理错误: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)


返回资金


 

解决方案

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

解决方案


这篇关于代理错误:System.Net.ProtocolViolationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 14:55