Who to change GET to POST Method in this code?

M

MARTIN LANNY

Hi everyone,

I am having a real trouble to figure out how to amend this 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
--------------------------------------------------------



This code gets the html source from any url you submit to it.
Can someone tell me what to do?

I tried to add this line:
objRequest.Method = "POST"
, right below the TRY, but nothing changed and I really don't know what
to do next, while all my customers are getting inpatient.

I am even willing to pay for the answer. THIS IS MY LAST RESOURCE.

Please let me know.

Martin
 
M

m.posseth

well then this should do the trick

Imports System.Text

Imports System.Net

Imports System.Collections.Specialized

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim myWebClient As New WebClient

Dim myNameValueCollection As New NameValueCollection

myNameValueCollection.Add("username", "john doe")

myNameValueCollection.Add("password", "12345")

Dim responseArray As Byte() =
myWebClient.UploadValues("https://www.google.com/adsense/login.do", "POST",
myNameValueCollection)

MsgBox(Encoding.UTF8.GetString(responseArray))

End Sub





i do not have a username and password to test it but i believe it should
work



regards



Michel Posseth [MCP]
 
M

MARTIN LANNY

Thanks to you, I have this code (below) to login to specified URL with
my username and
psw and get the resulting HTML code.
It's using "POST" method.


It works just fine when result (after login) is a page.


But in some cases, result isn't a page, but a download file.


In case the resulting page is a download file (csv file I am
downloading from google adsense) it will not read the response.
It gives me no result whatsoever.


Can someone tell me how to amend this code, so it also reads the
content of the file?


Public Function GetPageHTML(ByVal URL As String, Optional ByVal
TimeoutSeconds As Integer = 10) As String
Try
Dim myWebClient As New WebClient
Dim myNameValueCollection As New NameValueCollection
myNameValueCollection.Add("username", "joedoe")
myNameValueCollection.Add("password", "123456")
Dim responseArray As Byte() = myWebClient.UploadValues(URL,

"POST", myNameValueCollection)
GetPageHTML = Encoding.UTF8.GetString(responseArray)
Catch ex As Exception
' Error occured grabbing data, simply return nothing
Return ""
End Try
End Function


Thanks for every answer.
Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top