Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

828 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