Solved

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

Posted on 2014-01-09
4
1,400 Views
Last Modified: 2014-01-13
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
Comment
Question by:zavikon
  • 3
4 Comments
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 500 total points
ID: 39768651
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
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 39776697
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
 

Author Comment

by:zavikon
ID: 39776722
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
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 39776773
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Preface There are many applications where some computing systems need have their system clocks running synchronized within a small margin and eventually need to be in sync with the global time. There are different solutions for this, i.e. the W3…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now