![]() |
|
|
| Subject:
How to change the GET to POST method in this VB.NET code?
Category: Computers > Programming Asked by: enzomolinare-ga List Price: $50.00 |
Posted:
11 Sep 2005 05:42 PDT
Expires: 12 Sep 2005 06:27 PDT Question ID: 566737 |
Hi everyone,
I am having a real trouble to figure out how to amend this VB.NET code to
switch from GET to POST method.
--------------------------------------------------------
Public Function GetPageHTML(ByVal URL As String, Optional ByVal
TimeoutSeconds As Integer = 10) As String
' Retrieves the HTML from the specified URL, using a default
timeout of 10 seconds
Dim objRequest As Net.WebRequest
Dim objResponse As Net.WebResponse
Dim objStreamReceive As System.IO.Stream
Dim objEncoding As System.Text.Encoding
Dim objStreamRead As System.IO.StreamReader
Try
' Setup our Web request
objRequest = Net.WebRequest.Create(URL)
objRequest.Timeout = TimeoutSeconds * 1000
' Retrieve data from request
objResponse = objRequest.GetResponse
objStreamReceive = objResponse.GetResponseStream
objEncoding = System.Text.Encoding.GetEncoding("utf-8")
objStreamRead = New
System.IO.StreamReader(objStreamReceive, objEncoding)
' Set function return value
GetPageHTML = objStreamRead.ReadToEnd()
' Check if available, then close response
If Not objResponse Is Nothing Then
objResponse.Close()
End If
Catch
' Error occured grabbing data, simply return nothing
Return ""
End Try
End Function
--------------------------------------------------------
To bring you into the picture. This code gets the html source from any
url you submit to it. Problem is that the page I am grabbing the html
code from, requires url variables (name, psw) to be posted using POST
method. Current code automatically uses GET method.
Can someone tell me what to do?
I tried to add this line:
objRequest.Method = "POST"
, right below the TRY, but nothing changed.
THIS IS MY LAST RESOURCE.
Please let me know.
Joe |
|
| There is no answer at this time. |
|
| Subject:
Re: How to change the GET to POST method in this VB.NET code?
From: pmmbala1976-ga on 11 Sep 2005 08:29 PDT |
Hi
How about this code? from msdn..
' Create a new request to the mentioned URL.
Dim myWebRequest As WebRequest =
WebRequest.Create("http://www.contoso.com/codesnippets/next.asp")
' Create an instance of the RequestState and assign
'myWebRequest' to it's request field.
Dim myRequestState As New RequestState()
myRequestState.request = myWebRequest
myWebRequest.ContentType = "application/x-www-form-urlencoded"
' Set the 'Method' prperty to 'POST' to post data to a Uri.
myRequestState.request.Method = "POST"
myRequestState.request.ContentType =
"application/x-www-form-urlencoded"
' Start the Asynchronous 'BeginGetRequestStream' method call.
Dim r As IAsyncResult =
CType(myWebRequest.BeginGetRequestStream(AddressOf ReadCallback,
myRequestState), IAsyncResult)
' Assign the response object of 'WebRequest' to a
'WebResponse' variable.
Dim myWebResponse As WebResponse = myWebRequest.GetResponse()
Console.WriteLine(ControlChars.Cr + "The string entered
has been successfully posted to the Uri")
Console.WriteLine("Please wait for the response.......")
Dim streamResponse As Stream = myWebResponse.GetResponseStream()
Dim streamRead As New StreamReader(streamResponse)
Dim readBuff(256) As [Char]
Dim count As Integer = streamRead.Read(readBuff, 0, 256)
Console.WriteLine(ControlChars.Cr + "The contents of the
HTML page are ")
While count > 0
Dim outputData As New [String](readBuff, 0, count)
Console.WriteLine(outputData)
count = streamRead.Read(readBuff, 0, 256)
End While
' Close the Stream Object.
streamResponse.Close()
streamRead.Close()
allDone.WaitOne()
' Release the HttpWebResponse Resource.
myWebResponse.Close()
Catch e As WebException
Console.WriteLine(ControlChars.Cr + "WebException Caught!")
Console.WriteLine("Message :{0}", e.Message)
Catch e As Exception
Console.WriteLine(ControlChars.Cr + "Exception Caught!")
Console.WriteLine("Message :{0}", e.Message)
End Try
End Sub ' Main
Private Shared Sub ReadCallback(asynchronousResult As IAsyncResult)
Try
' State of request is set to asynchronous.
Dim myRequestState As RequestState =
CType(asynchronousResult.AsyncState, RequestState)
Dim myWebRequest2 As WebRequest = myRequestState.request
' End of the Asynchronus request.
Dim streamResponse As Stream =
myWebRequest2.EndGetRequestStream(asynchronousResult)
' Create a string that is to be posted to the uri.
Console.WriteLine(ControlChars.Cr + "Please enter a string
to be posted to (http://www.contoso.com/codesnippets/next.asp) Uri:")
Dim inputData As String = Console.ReadLine()
Dim postData As String = "firstone" + ChrW(61) + inputData
Dim encoder As New ASCIIEncoding()
' Convert the string into a byte array.
Dim ByteArray As Byte() = encoder.GetBytes(postData)
' Write data to the stream.
streamResponse.Write(ByteArray, 0, postData.Length)
streamResponse.Close()
allDone.Set()
Catch e As WebException
Console.WriteLine(ControlChars.Cr + "WebException Caught!")
Console.WriteLine("Message :{0}", e.Message)
Catch e As Exception
Console.WriteLine(ControlChars.Cr + "Exception Caught!")
Console.WriteLine("Message :{0}", e.Message)
End Try
End Sub ' ReadCallback
thanks
bala |
| Subject:
Re: How to change the GET to POST method in this VB.NET code?
From: enzomolinare-ga on 12 Sep 2005 06:24 PDT |
I had this code from msn help already, but I need to amend the code above. joe |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
| Search Google Answers for |
| Google Home - Answers FAQ - Terms of Service - Privacy Policy |