Webrequest via vb.net

tohnsm

New member
Joined
Sep 20, 2007
Messages
4
Programming Experience
1-3
Hi.

In a program I am doing, I am trying to POST to a website. However, prior to that I have also use GET request. That is:

VB.NET:
request = CType(WebRequest.Create("...."), HttpWebRequest)

request.Method = "GET"
request.CookieContainer = New CookieContainer() 
cookie container ookie = request.CookieContainer 

response = CType(request.GetResponse(), HttpWebResponse) 
sr = New StreamReader(response.GetResponseStream())        

'processing of sr found here, next POST to the same WebRequest created 
'earlier

request.Method = "POST"
request.CookieContainer = ookie
request.ContentType = "Content-Type: application/x-www-form-urlencoded"

postData = request.GetRequestStream()   ==> error occurs here
postData.Write(buffer,0,buffer.Length)       
postData.Close()
The error prompted is:
Unhandled Exception: System.Net.ProtocolViolationException: Cannot send a content-body with this verb-type.
at System.Net.HttpWebRequest.CheckProtocol<Boolean onRequestStream>
at System.Net.HttpWebRequest.GetRequestStream()
Could any one please help?
 
Last edited by a moderator:
You can't change the request once executed. (You also can't execute the same request twice, ie calling GetResponse twice on same object.) Create a new request before each call to GetResponse/-Stream.
 
Back
Top