.NETGURU
HttpWebRequest - GET Method
Messages   Related Types
This message was discovered on ASPFriends.com 'ngfx-io' list.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.

Jim Davis
I'd like to GET the page inside an HTTPWEBREQUEST Class, but it doesn't seem
to like GET as the Method.

Keeps displaying the following error.

Cannot send a content-body with this verb-type.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.Net.ProtocolViolationException: Cannot send a
content-body with this verb-type.

Source Error:

Line 25: ' Add body to request
Line 26: objRequest.ContentLength = arrRequest.Length
Line 27: strmRequest = objRequest.GetRequestStream()
Line 28: strmRequest.Write( arrRequest, 0, arrRequest.Length )
Line 29: strmRequest.Close()

Sub Button_Click( s As Object, e As EventArgs )
Dim objRequest As HttpWebRequest
Dim strRequest As String
Dim arrRequest As Byte()
Dim objUTF8Encoding As UTF8Encoding
Dim strmRequest As Stream
Dim objResponse As HttpWebResponse
Dim srResponse As StreamReader

' Initialize request object
objRequest = CType( WebRequest.Create( txtMessage.Text.ToString() ),
HttpWebRequest )
objRequest.Method = "GET"
objRequest.ContentType = "application/x-www-form-urlencoded"

' Create request body
strRequest = "Message=" & Server.UrlEncode( txtMessage.Text )
objUTF8Encoding = New UTF8Encoding
arrRequest = objUTF8Encoding.GetBytes( strRequest )

' Add body to request
objRequest.ContentLength = arrRequest.Length
strmRequest = objRequest.GetRequestStream()
strmRequest.Write( arrRequest, 0, arrRequest.Length )
strmRequest.Close()

' Get response
objResponse = objRequest.GetResponse()
srResponse = New StreamReader( objResponse.GetResponseStream(),
Encoding.ASCII )
lblResponse.Text = srResponse.ReadToEnd()
srResponse.Close()
End Sub

Ideas?
Thanks,

Jim Davis
.NET Web Developer

Reply to this message...
 
    
Devin Rader
I'm not sure you can do this with the HttpWebRequest classes. I think you
might have to go a little lower and either use the TcpClient or a Socket.

Devin

-----Original Message-----
From: Jim Davis [mailto:Click here to reveal e-mail address]
Sent: Monday, May 06, 2002 3:54 PM
To: ngfx-io
Subject: [ngfx-io] HttpWebRequest - GET Method

I'd like to GET the page inside an HTTPWEBREQUEST Class, but it doesn't seem
to like GET as the Method.

Keeps displaying the following error.

Cannot send a content-body with this verb-type.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.Net.ProtocolViolationException: Cannot send a
content-body with this verb-type.

Source Error:

Line 25: ' Add body to request
Line 26: objRequest.ContentLength = arrRequest.Length
Line 27: strmRequest = objRequest.GetRequestStream()
Line 28: strmRequest.Write( arrRequest, 0, arrRequest.Length )
Line 29: strmRequest.Close()

Sub Button_Click( s As Object, e As EventArgs )
Dim objRequest As HttpWebRequest
Dim strRequest As String
Dim arrRequest As Byte()
Dim objUTF8Encoding As UTF8Encoding
Dim strmRequest As Stream
Dim objResponse As HttpWebResponse
Dim srResponse As StreamReader

' Initialize request object
objRequest = CType( WebRequest.Create( txtMessage.Text.ToString() ),
HttpWebRequest )
objRequest.Method = "GET"
objRequest.ContentType = "application/x-www-form-urlencoded"

' Create request body
strRequest = "Message=" & Server.UrlEncode( txtMessage.Text )
objUTF8Encoding = New UTF8Encoding
arrRequest = objUTF8Encoding.GetBytes( strRequest )

' Add body to request
objRequest.ContentLength = arrRequest.Length
strmRequest = objRequest.GetRequestStream()
strmRequest.Write( arrRequest, 0, arrRequest.Length )
strmRequest.Close()

' Get response
objResponse = objRequest.GetResponse()
srResponse = New StreamReader( objResponse.GetResponseStream(),
Encoding.ASCII )
lblResponse.Text = srResponse.ReadToEnd()
srResponse.Close()
End Sub

Ideas?
Thanks,

Jim Davis
.NET Web Developer

| [ngfx-io] member Click here to reveal e-mail address = YOUR ID
| http://www.aspfriends.com/aspfriends/ngfx-io.asp = JOIN/QUIT
Reply to this message...
 
    
Mitch Denny (VIP)
Jim,

Try using a POST. When using the GET method the input
data must be URL encoded, not encoded in the payload
like you do with a POST. Here is an example of a valid
GET request via HTTP:

    C: GET /test.aspx?z=a&y=b&x=c HTTP/1.0
    C:
    C:

Notice how the values are encoded in the GET request.    

----------------------------------------
- Mitch Denny
- Click here to reveal e-mail address
- +61 (414) 610-141
-

-----Original Message-----
From: Jim Davis [mailto:Click here to reveal e-mail address]
Sent: Tuesday, 7 May 2002 06:54
To: ngfx-io
Subject: [ngfx-io] HttpWebRequest - GET Method

I'd like to GET the page inside an HTTPWEBREQUEST Class, but it doesn't
seem to like GET as the Method.

Keeps displaying the following error.

Cannot send a content-body with this verb-type.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.Net.ProtocolViolationException: Cannot send a
content-body with this verb-type.

Source Error:

Line 25: ' Add body to request
Line 26: objRequest.ContentLength = arrRequest.Length
Line 27: strmRequest = objRequest.GetRequestStream()
Line 28: strmRequest.Write( arrRequest, 0, arrRequest.Length )
Line 29: strmRequest.Close()

Sub Button_Click( s As Object, e As EventArgs )
Dim objRequest As HttpWebRequest
Dim strRequest As String
Dim arrRequest As Byte()
Dim objUTF8Encoding As UTF8Encoding
Dim strmRequest As Stream
Dim objResponse As HttpWebResponse
Dim srResponse As StreamReader

' Initialize request object
objRequest = CType( WebRequest.Create( txtMessage.Text.ToString() ),
HttpWebRequest )
objRequest.Method = "GET"
objRequest.ContentType = "application/x-www-form-urlencoded"

' Create request body
strRequest = "Message=" & Server.UrlEncode( txtMessage.Text )
objUTF8Encoding = New UTF8Encoding
arrRequest = objUTF8Encoding.GetBytes( strRequest )

' Add body to request
objRequest.ContentLength = arrRequest.Length
strmRequest = objRequest.GetRequestStream()
strmRequest.Write( arrRequest, 0, arrRequest.Length )
strmRequest.Close()

' Get response
objResponse = objRequest.GetResponse()
srResponse = New StreamReader( objResponse.GetResponseStream(),
Encoding.ASCII )
lblResponse.Text = srResponse.ReadToEnd()
srResponse.Close()
End Sub

Ideas?
Thanks,

Jim Davis
.NET Web Developer

| [ngfx-io] member Click here to reveal e-mail address = YOUR ID
| http://www.aspfriends.com/aspfriends/ngfx-io.asp = JOIN/QUIT

Reply to this message...
 
 
System.EventArgs
System.IO.StreamReader
System.Net.HttpWebRequest
System.Net.HttpWebResponse
System.Net.ProtocolViolationException
System.Net.Sockets.TcpClient
System.Net.WebRequest
System.Text.Encoding
System.Text.UTF8Encoding




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification