Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1570
  • Last Modified:

Https POST from Vb.net to PHP to get XML file

Hello, I have a Vb.Net application that is running the compact dot net framework on a Windows mobile device.  I am need to download an XML file from a PHP site by making an Https POST with a couple of variables.  

1. Following is the code that I have so far, but I am not sure how to send my two variables (user and pass) and their values through the POST.  

2. I am not sure that my code is proper to receive and save the XML file that is being returned.

' Create a request using a URL that can receive a post. 
        Dim request As WebRequest =   WebRequest.Create("https://www.mysite.com/get_categories.php")
        ' Set the Method property of the request to POST.
        request.Method = "POST"
        ' Create POST data and convert it to a byte array.
        Dim postData As String = "This is a test that posts this string to a Web server."
        Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
        ' Set the ContentType property of the WebRequest.
        request.ContentType = "application/x-www-form-urlencoded"
        ' Set the ContentLength property of the WebRequest.
        request.ContentLength = byteArray.Length
        ' Get the request stream.
        Dim dataStream As Stream = request.GetRequestStream()
        ' Write the data to the request stream.
        dataStream.Write(byteArray, 0, byteArray.Length)
        ' Close the Stream object.
        dataStream.Close()
        ' Get the response.
        Dim response As WebResponse = request.GetResponse()
        ' Display the status.
        Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)
        ' Get the stream containing content returned by the server.
        dataStream = response.GetResponseStream()
        ' Open the stream using a StreamReader for easy access.
        Dim reader As New StreamReader(dataStream)
        ' Read the content.
        Dim responseFromServer As String = reader.ReadToEnd()
        ' Display the content.
        Console.WriteLine(responseFromServer)
        ' Clean up the streams.
        reader.Close()
        dataStream.Close()
        response.Close()

Open in new window

0
zavikon
Asked:
zavikon
  • 3
1 Solution
 
gr8gonzoConsultantCommented:
This line defines your POST data:

Dim postData As String = "This is a test that posts this string to a Web server."

You can change this to a simple key/pair string, like this:

Dim postData As String = "user=joesmith&password=secret123"
0
 
gr8gonzoConsultantCommented:
Just for future reference, when you accept an answer and give it a grade less than "A", you usually need to indicate why. Anything less than an "A" usually means that the answer was not complete or correct, and if you don't provide feedback, we cannot help you fix that answer (also, so others can see the updates).
0
 
zavikonAuthor Commented:
That you for pointing that out.  I graded it as "good" because it only solved 1 of my issues, whereas I had 2 issues within my question.
0
 
gr8gonzoConsultantCommented:
Ah, sorry, I missed the second question somehow (maybe because there was no corresponding code). In any event, the code snippet you have shows the data being received being put into a string variable "responseFromServer".

Once you have that string, it doesn't really matter if it's XML or just normal text. You can save a string to a file with File.WriteAllText:

http://msdn.microsoft.com/en-us/library/ms143375(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now