Solved

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

Posted on 2014-01-09
4
1,424 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

863 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

24 Experts available now in Live!

Get 1:1 Help Now