1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Help with Submitting a Form

Discussion in 'Visual Basic .NET' started by fpforum, Jan 9, 2013.

  1. fpforum

    fpforum Junior Member

    Joined:
    Apr 22, 2008
    Messages:
    117
    Likes Received:
    4
    Home Page:
    Hey everyone..I'd like to write a program which automatically goes to this site: comicalphotos*com/submit.php and submits the form via either WebClient or HTTPWebRequest..

    Which method would be better for this?

    On my form I have three text boxes - one text box has an openfiledialog box where they would select the image they want to be uploaded..How would I go about passing that image from their computer into the form??

    Any help on this would be greatly appreciated!
     
  2. fpforum

    fpforum Junior Member

    Joined:
    Apr 22, 2008
    Messages:
    117
    Likes Received:
    4
    Home Page:
    I managed to find some code and put a small sub together..It seams to connect to the site but for some reason..I don't think the form is getting submitted...When I look at the response I'm getting back it doesn't have "successful" or "unsuccessful" in it..

    Code:
        Public Sub UploadFile(ByVal localFile As String)
            Dim Req As HttpWebRequest = DirectCast(WebRequest.Create(New Uri(uploadLink)), HttpWebRequest)
            Req.Method = "POST"
            Req.Referer = (uploadLink)
            Dim Boundary As String = "----" & DateTime.Now.Ticks.ToString
            Req.ContentType = "multipart/form-data; boundary=" & Boundary
            Dim NewLine As String = Environment.NewLine
            Dim PostData As String = NewLine & Boundary & NewLine
            PostData &= "Content-Disposition: form-data; title=" & postTitle & "; source=" & sourceUrl & "; picture=" & Chr(34) & IO.Path.GetFileName(localFile).ToString & Chr(34) & NewLine
            Select Case IO.Path.GetExtension(localFile).ToLower
                Case ".gif"
                    PostData &= "Content-Type: image/gif" & NewLine
                Case ".jpg"
                    PostData &= "Content-Type: image/jpeg" & NewLine
                Case ".png"
                    PostData &= "Content-Type: image/png" & NewLine
            End Select
            Dim HeaderBytes() As Byte = System.Text.Encoding.UTF8.GetBytes(PostData)
    
            MsgBox(Boundary)
            MsgBox(PostData)
    
            Dim Bytes() As Byte = File.ReadAllBytes(localFile)
            Req.ContentLength = HeaderBytes.Length + Bytes.Length
            MsgBox("Content Length : " & Req.ContentLength)
    
            Dim RunningTotal As Integer = 0
            Using ReqStream As Stream = Req.GetRequestStream
                Using fs As New FileStream(localFile, FileMode.Open, FileAccess.Read)
    
                    Dim Buffer(4096) As Byte
                    Dim BytesRead As Integer = fs.Read(Buffer, 0, Buffer.Length)
                    ReqStream.Write(HeaderBytes, 0, HeaderBytes.Length)
    
                    While BytesRead > 0
                        RunningTotal += BytesRead
                        ReqStream.Write(Buffer, 0, BytesRead)
                        BytesRead = fs.Read(Buffer, 0, Buffer.Length)
    
                        'Console.SetCursorPosition(0, Console.CursorTop)
                        'Console.Write(RunningTotal & "/" & fs.Length)
    
                    End While
    
                End Using
    
                ReqStream.Close()
            End Using
            Dim Res As HttpWebResponse = DirectCast(Req.GetResponse, HttpWebResponse)
            Dim Reader As New StreamReader(Res.GetResponseStream)
            Dim InformationA As String = String.Empty
            InformationA = Reader.ReadToEnd
            informationB = InformationA
    
        End Sub
    
     
  3. Psychop1

    Psychop1 Registered Member

    Joined:
    Jul 30, 2010
    Messages:
    63
    Likes Received:
    19
    I haven't tested your code yet, but I don't see a cookie container. Try adding req.cookiecontainer = new cookiecontainer. See what results you get.
     
  4. fpforum

    fpforum Junior Member

    Joined:
    Apr 22, 2008
    Messages:
    117
    Likes Received:
    4
    Home Page:
    Thanks for the suggestion Psychop1..I added that in but it still didn't post. Any other suggestions? I've added messageboxes in there to view the data that should be posting and it all looks correct..Not really sure why the heck it won't submit..I notice in the form located on the website it shows this:
    <form action="submit.php#subpicture" method="post" enctype="multipart/form-data" id="picsubmit">

    Do I need to put the action or ID in there somewhere too?


    =UPDATE=
    by the way..this is what the header looks like when submitting through IE
    Code:
    POST /submit.php HTTP/1.1
    Accept: text/html, application/xhtml+xml, */*
    Referer: http://www.comicalphotos*com/submit.php
    Accept-Language: en-US
    User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
    Content-Type: multipart/form-data; boundary=---------------------------7dd36813bb0c3c
    Accept-Encoding: gzip, deflate
    Host: www.comicalphotos*com
    Content-Length: 88320
    Connection: Keep-Alive
    Pragma: no-cache
    Cookie: PHPSESSID=543n9lu46f3mfrscj90bd7pf44; HstCfa1968538=1358849786678; HstCla1968538=1358849786678; HstCmu1968538=1358849786678; HstPn1968538=1; HstPt1968538=1; HstCnv1968538=1; HstCns1968538=1; __unam=e0288e1-13c61c24503-77f8a836-1
    
    and...

    This is what my header looks like when submitting through my program in debug
    Code:
    POST /submit.php HTTP/1.1
    Accept: text/html, application/xhtml+xml, */*
    Referer: http://www.comicalphotos*com/submit.php
    Accept-Language: en-US
    User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
    Content-Type: multipart/form-data; boundary=---------------------------634944273894738229
    Accept-Encoding: gzip, deflate
    Pragma: no-cache
    Host: www.comicalphotos*com
    Content-Length: 10296
    Expect: 100-continue
    Connection: Keep-Alive
    
     
    Last edited: Jan 22, 2013
  5. gimme4free

    gimme4free Executive VIP Jr. VIP Premium Member

    Joined:
    Oct 22, 2008
    Messages:
    1,880
    Likes Received:
    1,932
    Make sure that your multipart POST data is the same also, use Fiddler or Charles to check the data that is sent between program - browser.
     
  6. hatemachine

    hatemachine Regular Member

    Joined:
    Jan 14, 2011
    Messages:
    271
    Likes Received:
    1,138
    I see no cookies in your header, that's prolly the issue or what gimme4free said.