Solved

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

Posted on 2014-01-09
4
1,463 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
[X]
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
  • 3
4 Comments
 
LVL 35

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 35

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 35

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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 count occurrences of each item in an array.

730 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