digitaldrew
Well-known member
- Joined
- Nov 10, 2012
- Messages
- 167
- Programming Experience
- Beginner
I'm trying to have a program go through a list of websites and submit a form. The form has 3 fields: One is for a Title, one is to browse and select an image, and the third is to enter a website Url. I'd like to use HTTP web request to go through and submit the form on each page.
In my program I've created 3 text boxes. One of the text boxes has a button next to it with openfiledialog where the user selects the image they want to upload and it then inputs that location into the textbox.
I know I'm doing something wrong here because I'm passing the file location from the textbox as a string and I am certain it can't be that easy..lol. Regardless, my program seams to go through and work fine, but as expected, it tells me that each submission was unsuccessful. Here is some code:
When the doworker kicks off it should select the first checked website in the list and call the WRequest function.
Here is the WRequest function:
What am I doing wrong here? How would I go about passing the file that was selected in the second textbox I created? Any help would be appriciated!
In my program I've created 3 text boxes. One of the text boxes has a button next to it with openfiledialog where the user selects the image they want to upload and it then inputs that location into the textbox.
I know I'm doing something wrong here because I'm passing the file location from the textbox as a string and I am certain it can't be that easy..lol. Regardless, my program seams to go through and work fine, but as expected, it tells me that each submission was unsuccessful. Here is some code:
When the doworker kicks off it should select the first checked website in the list and call the WRequest function.
VB.NET:
Private Sub SubmitThread_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles SubmitThread.DoWork
Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
For i As Integer = 0 To Me.lstSubmissionSites.CheckedItems.Count - 1
r = i
Dim selectedSite As String = String.Empty
selectedSite = lstSubmissionSites.Items(i).ToString
If lstSubmissionSites.InvokeRequired Then
lstSubmissionSites.Invoke(Sub() lstSubmissionSites.SelectedIndex = r)
End If
If TestThread.CancellationPending = True Then
e.Cancel = True
Exit For
Else
WRequest(selectedSite, "POST", "title=" & postTitle & "&picture=" & strFileName & "&source=" & sourceUrl & "")
worker.ReportProgress(0, streamResponse)
End If
Next
End Sub
Here is the WRequest function:
VB.NET:
Function WRequest(URL As String, method As String, POSTdata As String) As String
Try
Dim hwrequest As Net.HttpWebRequest = Net.Webrequest.Create(URL)
hwrequest.Accept = "*/*"
hwrequest.AllowAutoRedirect = True
hwrequest.UserAgent = "http_requester/0.1"
hwrequest.Timeout = 5000
hwrequest.Method = method
If hwrequest.Method = "POST" Then
hwrequest.ContentType = "application/x-www-form-urlencoded"
Dim encoding As New Text.ASCIIEncoding() 'Use UTF8Encoding for XML requests
Dim postByteArray() As Byte = encoding.GetBytes(POSTdata)
hwrequest.ContentLength = postByteArray.Length
Dim postStream As IO.Stream = hwrequest.GetRequestStream()
postStream.Write(postByteArray, 0, postByteArray.Length)
postStream.Close()
End If
Dim hwresponse As Net.HttpWebResponse = hwrequest.GetResponse()
If hwresponse.StatusCode = Net.HttpStatusCode.OK Then
Dim responseStream As IO.StreamReader = _
New IO.StreamReader(hwresponse.GetResponseStream())
streamResponse = responseStream.ReadToEnd()
End If
hwresponse.Close()
Catch e As Exception
responseData = "An error occurred: " & e.Message
End Try
Return responseData
End Function
What am I doing wrong here? How would I go about passing the file that was selected in the second textbox I created? Any help would be appriciated!