• 0

[VB.NET] Http header and requests help


Question

Hello,

I'm going to be a working on a tool to log into a site and upload files, the tool will send requests to the site ( everything is coded server-side already ).

What I'm concerned with, the tool will prompt the user for Username/Password when he run it, then when the user make requests, the tool will have to check if the session is valid ( the current cookie he have is valid ).

How can I do that? I'm new to VB.NET.. What's the best way to login to the site? After I login, will the tool have the cookie automatically? And how I can check that the cookie is valid when a request is initiated?

The site is not an ASP site. Mostly PHP.

Thanks in advance

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

I tried to write the code to simulate a POST request from my application to the site but it's not logging me in

Public Shared Sub Main(ByVal args() As String)
		' Create a request using a URL that can receive a post. 
		Dim request As WebRequest = WebRequest.Create("http://www.MYSITE.net")
		' 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 = "username=USER&password=PASS&login=Log+in%21"

		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("Status: " + 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("Page contents: " + responseFromServer)
		' Clean up the streams.
		reader.Close()
		dataStream.Close()
		response.Close()
		Console.ReadLine()






	End Sub 'Main

any idea why ?

Link to comment
Share on other sites

  • 0

Your script checks for a session after you login, all this is doing is posting a form without a session/cookie and returning the results.

Use the CookieContainer object to maintain state.

Link to comment
Share on other sites

  • 0

Thanks a lot General.

I added these 2 lines and it worked

		Dim cookies As New CookieContainer
		request.CookieContainer = cookies

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.