Unable to post data using HttpWebRequest when using variable

Free6

Newbie
Joined
Nov 21, 2016
Messages
47
Reaction score
1
Hi guys,

I'm stuck as I can't post data using HttpWebRequest on below body.

The problem is I need to dynamcally insert data, so I have to use variable.
I use variable and it works right but only on "Title" (adTitlEncoded - on the below body ) that doesn't allow me to use variable.

If I insert manually a text on development mode and run the program, its posts nicely but when I use a variable to substitute the text (adTitlEncoded) as I need to load data in run-time, it doesn't post data.

Not sure why, Can you please help?

Code:
body = "locationId=" & locPesten2 & "&categoryId=" & categPost & "&machineId=dfe9c439-43d1-479e-a0fa-548e217ec6d7-15f7c4dc6d3&completenessPercentage=71&ForSaleBy=&Title=" & "" & adTitlEncoded & "" & "&Description=" & jobDescriptEncoded & "&WebSiteUrl=&u=images.jpg&u=images_002.jpg&u=images_021.jpg&profilePicture=&picturesThumb=https%253A%252F%252Fi.ebayimg.com%252F00%252Fs%252FMjI3WDIyMg%253D%253D%252Fz%252FdMwAAOSwry1aAHH9%252F%2524_14.JPG&pictures=https%253A%252F%252Fi.ebayimg.com%252F00%252Fs%252FMTk0WDI1OQ%253D%253D%252Fz%252F8wYAAOSwR2RaAHH%7E%252F%2524_18.JPG&priceTypes=FIXED&Price=&currencyValues=ZAR&UserName=Alice&Email=aliyababy9%40gmail.com&Phone=&latitude=&longitude=&adminAreaName=&addressConfidenceLevel=&countryCode=&street=&locality=&Address="
 
Hard to tell without posting full code , did you tried it with WebClient.UploadValues and NameValueCollection ?
 
It's really a full full code. Please find it below

Indeed the app was working perfectly well, it's been now 2 years since it's working without problem,
since before yesterday, I'm trying to send a POST and couldn't... so I checked everything and realized that
I'm unable to send post to the server.

I think the server I'm connecting to has been updated (the website) that's why !!

Can you please help me out?

Code:
Imports System.Net
Imports System.Text
Imports System.IO
Imports System.Diagnostics
Imports System.IO.Compression
Imports System.Xml

Imports System.Net.WebUtility

'Post Ad

Module modPostAd

    Public logincookiePost As CookieContainer
    Dim logincookstring As String
    Public ebayPictName As String
    Public AdIdtoSave As String
    Public pathAdsforAdId As String
    Dim body As String

    Public pic1, pic2, pic3, pic4, pic5, pic6, pic7, pic8, pic9, pic10, pic11, pic12 As String  'Store picture name to be put in Body


    'Calls request functions sequentially.
    Sub MakeRequests2()
        Dim response As HttpWebResponse
        Dim responseText As String
        Dim myadId As String
        'Dim body As String

        If Request_www_gumtree_co_za(response) Then
            'Success, possibly use response.
            responseText = ReadResponse(response)

            myadId = responseText

            If myadId.Contains("adId=") Then

                Dim res As String = myadId.Substring(myadId.IndexOf("adId="))

                AdIdtoSave = res.Substring(0, res.IndexOf("&"))

                Form1.TextBox1.Text = AdIdtoSave

                pathAdsforAdId = pathFilenameAdXML & theList

                'Save AdId into XML File
                SaveAdIdtoDataToXmlFile()

            End If

            response.Close()

            StopAllThread()

            listFileToPost.Remove(theList)

            'StopAllThread()

            onlypictNameM.Clear()

            onlypictNameP.Clear()

            PostAd()

        Else
            'Failure, cannot use response.
        End If
    End Sub

    'Returns the text contained in the response.  For example, the page HTML.  Only handles the most common HTTP encodings.
    Public Function ReadResponse(response As HttpWebResponse) As String
        Using responseStream = response.GetResponseStream()
            Dim streamToRead As Stream = responseStream
            If response.ContentEncoding.ToLower().Contains("gzip") Then
                streamToRead = New GZipStream(streamToRead, CompressionMode.Decompress)
            ElseIf response.ContentEncoding.ToLower().Contains("deflate") Then
                streamToRead = New DeflateStream(streamToRead, CompressionMode.Decompress)
            End If

            Using streamReader = New StreamReader(streamToRead, Encoding.UTF8)
                Return streamReader.ReadToEnd()
            End Using
        End Using
    End Function

    ''' <summary>
    ''' Tries to request the URL: https://www.gumtree.co.za/post.html
    ''' </summary>
    ''' <param name="response">After the function has finished, will possibly contain the response to the request.</param>
    ''' <returns>True if the request was successful; false otherwise.</returns>
    Function Request_www_gumtree_co_za(ByRef response As HttpWebResponse) As Boolean
        response = Nothing

        Try
            'Create request to URL.
            Dim request As HttpWebRequest = DirectCast(WebRequest.Create("https://www.gumtree.co.za/post.html"), HttpWebRequest)
            Dim tempCookies As New CookieContainer
            Dim usrname As String       'Retrieve Username from Email

            'Retrieve Username from Email (Extract only part before @ to be used as Gumtree Username)
            usrname = gumtrEmail.Split("@")(0)
      
            'request.AllowAutoRedirect = False
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:56.0) Gecko/20100101 Firefox/56.0"
            request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
            request.Headers.Set(HttpRequestHeader.AcceptLanguage, "en-US,en;q=0.5")
            request.Headers.Set(HttpRequestHeader.AcceptEncoding, "gzip, deflate, br")
            request.Referer = "https://www.gumtree.co.za/post.html"
            request.ContentType = "application/x-www-form-urlencoded"
            'request.Headers.Set(HttpRequestHeader.Cookie, "machguid=dfe9c439-43d1-479e-a0fa-548e217ec6d7-15f7c4dc6d3; optimizelyEndUserId=oeu1509619070147r0.4715000061674772; __utma=29192163.901405201.1509619074.1509630807.1509638806.4; __utmz=29192163.1509619074.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); AMCV_A71B5B5B54F607AB0A4C98A2%40AdobeOrg=-1176276602%7CMCMID%7C65714495179233650412536812766843753563%7CMCAAMLH-1510223876%7C6%7CMCAAMB-1510243738%7C1jyDEgZ_dl1M-NaoXGeH6mSpkWB68KrTxI0X54i3vipydEg%7CMCOPTOUT-1509646009s%7CNONE%7CMCAID%7CNONE%7CMCCIDH%7C-1409438482; _ga=GA1.3.901405201.1509619074; _gid=GA1.3.1241843946.1509619077; aam_uuid=65748732469562759802537865404489035238; _em_vt=d0423fbc2630d6efd2289e3863d459faf53ecda405-8858597559fb433d; _em_d=1509619187; __gads=ID=aa6f1799ccc6c828:T=1509628384:S=ALNI_MZH27T8s7rP2tMdUjYR1T7nVDj_lw; anonUsrId=02ec85b1-2db8-4351-8e16-c201298bb283; __utmc=29192163; AMCVS_A71B5B5B54F607AB0A4C98A2%40AdobeOrg=1; locId=3100287; doublePost=false; seller=PC; __utmb=29192163.8.10.1509638806; _em_v=51446cb85ade29b46125ef6b406759fb4251697fd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QzODcwOXEAfgAqcHEAfgAe__v1__B4C7E19DCD6DA6409677; bt_logged_in=true; _gali=postSubmit; eCG_eh=ec=PostAdDetails:ea=PostAdFreeAttempt:el=undefined:pt=PostAdDetails:url=https://www.gumtree.co.za/post.html:cc=-1:lc=-1:aid=:; _gat=1")
            'request.KeepAlive = True
            request.Headers.Add("Upgrade-Insecure-Requests", "1")

            request.CookieContainer = logincookieTest

          
            'After Conversion
            Dim jobDescriptEncoded As String = Nothing

            Dim adTitlEncoded As String = Nothing

            
            jobDescriptEncoded = WebUtility.UrlEncode(jobDescript)

            adTitlEncoded = WebUtility.UrlEncode(adTitl)


            request.Method = "POST"
            request.ServicePoint.Expect100Continue = False

            
            ''''body = "locationId=" & locWesten2 & "&categoryId=" & categPost & "&machineId=dfe9c439-43d1-479e-a0fa-548e217ec6d7-15f7c4dc6d3&completenessPercentage=71&ForSaleBy=&Title=" & "" & adTitlEncoded & "" & "&Description=" & jobDescriptEncoded & "&WebSiteUrl=&u=images.jpg&u=images_002.jpg&u=images_003.jpg&u=images_004.jpg&u=images_005.jpg&u=images_006.jpg&u=images_007.jpg&u=images_008.jpg&u=images_009.jpg&u=images_010.jpg&u=images_011.jpg&u=images_012.jpg&u=images_013.jpg&u=images_014.jpg&u=images_015.jpg&u=images_016.jpg&u=images_017.jpg&u=images_018.jpg&u=images_019.jpg&u=images_020.jpg&u=images_021.jpg&profilePicture=&picturesThumb=https%253A%252F%252Fi.ebayimg.com%252F00%252Fs%252FMjI3WDIyMg%253D%253D%252Fz%252FdMwAAOSwry1aAHH9%252F%2524_14.JPG&pictures=https%253A%252F%252Fi.ebayimg.com%252F00%252Fs%252FMjI3WDIyMg%253D%253D%252Fz%252FdMwAAOSwry1aAHH9%252F%2524_18.JPG&picturesThumb=https%253A%252F%252Fi.ebayimg.com%252F00%252Fs%252FMjAwWDI1Mg%253D%253D%252Fz%252FVvoAAOSwNkJaAHH9%252F%2524_14.JPG&pictures=https%253A%252F%252Fi.ebayimg.com%252F00%252Fs%252FMjAwWDI1Mg%253D%253D%252Fz%252FVvoAAOSwNkJaAHH9%252F%2524_18.JPG&picturesThumb=https%253A%252F%252Fi.ebayimg.com%252F00%252Fs%252FMTYwWDMxNQ%253D%253D%252Fz%252FtHYAAOSwZaNaAHH9%252F%2524_14.JPG&pictures=https%253A%252F%252Fi.ebayimg.com%252F00%252Fs%252FMTYwWDMxNQ%253D%253D%252Fz%252FtHYAAOSwZaNaAHH9%252F%2524_18.JPG&picturesThumb=https%253A%252F%252Fi.ebayimg.com%252F00%252Fs%252FMjM2WDIxNA%253D%253D%252Fz%252FxX8AAOSwDFBaAHH9%252F%2524_14.JPG&pictures=https%253A%252F%252Fi.ebayimg.com%252F00%252Fs%252FMjM2WDIxNA%253D%253D%252Fz%252FxX8AAOSwDFBaAHH9%252F%2524_18.JPG&picturesThumb=https%253A%252F%252Fi.ebayimg.com%252F00%252Fs%252FMTk0WDI1OQ%253D%253D%252Fz%252FysgAAOSwK%7ERaAHH-%252F%2524_14.JPG&pictures=https%253A%252F%252Fi.ebayimg.com%252F00%252Fs%252FMTk0WDI1OQ%253D%253D%252Fz%252FysgAAOSwK%7ERaAHH-%252F%2524_18.JPG&picturesThumb=https%253A%252F%252Fi.ebayimg.com%252F00%252Fs%252FMTc1WDI4OA%253D%253D%252Fz%252FQN4AAOSw6WFaAHH-%252F%2524_14.JPG&pictures=https%253A%252F%252Fi.ebayimg.com%252F00%252Fs%252FMTc1WDI4OA%253D%253D%252Fz%252FQN4AAOSw6WFaAHH-%252F%2524_18.JPG&picturesThumb=https%253A%252F%252Fi.ebayimg.com%252F00%252Fs%252FMTk0WDI1OQ%253D%253D%252Fz%252FkxUAAOSwFSxaAHH%7E%252F%2524_14.JPG&pictures=https%253A%252F%252Fi.ebayimg.com%252F00%252Fs%252FMTk0WDI1OQ%253D%253D%252Fz%252FkxUAAOSwFSxaAHH%7E%252F%2524_18.JPG&picturesThumb=https%253A%252F%252Fi.ebayimg.com%252F00%252Fs%252FMjI1WDIyNQ%253D%253D%252Fz%252FOioAAOSwHEhaAHH%7E%252F%2524_14.JPG&pictures=https%253A%252F%252Fi.ebayimg.com%252F00%252Fs%252FMjI1WDIyNQ%253D%253D%252Fz%252FOioAAOSwHEhaAHH%7E%252F%2524_18.JPG&picturesThumb=https%253A%252F%252Fi.ebayimg.com%252F00%252Fs%252FMjA4WDI0Mg%253D%253D%252Fz%252FngQAAOSw%7ERVaAHH%7E%252F%2524_14.JPG&pictures=https%253A%252F%252Fi.ebayimg.com%252F00%252Fs%252FMjA4WDI0Mg%253D%253D%252Fz%252FngQAAOSw%7ERVaAHH%7E%252F%2524_18.JPG&picturesThumb=https%253A%252F%252Fi.ebayimg.com%252F00%252Fs%252FMjUxWDIwMQ%253D%253D%252Fz%252FU0gAAOSw%7EvpaAHH%7E%252F%2524_14.JPG&pictures=https%253A%252F%252Fi.ebayimg.com%252F00%252Fs%252FMjUxWDIwMQ%253D%253D%252Fz%252FU0gAAOSw%7EvpaAHH%7E%252F%2524_18.JPG&picturesThumb=https%253A%252F%252Fi.ebayimg.com%252F00%252Fs%252FMTk0WDI1OQ%253D%253D%252Fz%252FtIMAAOSwZaNaAHH%7E%252F%2524_14.JPG&pictures=https%253A%252F%252Fi.ebayimg.com%252F00%252Fs%252FMTk0WDI1OQ%253D%253D%252Fz%252FtIMAAOSwZaNaAHH%7E%252F%2524_18.JPG&picturesThumb=https%253A%252F%252Fi.ebayimg.com%252F00%252Fs%252FMTk0WDI1OQ%253D%253D%252Fz%252F8wYAAOSwR2RaAHH%7E%252F%2524_14.JPG&pictures=https%253A%252F%252Fi.ebayimg.com%252F00%252Fs%252FMTk0WDI1OQ%253D%253D%252Fz%252F8wYAAOSwR2RaAHH%7E%252F%2524_18.JPG&priceTypes=FIXED&Price=&currencyValues=ZAR&UserName=Alice&Email=alicebongiwe77%40gmail.com&Phone=&latitude=&longitude=&adminAreaName=&addressConfidenceLevel=&countryCode=&street=&locality=&Address="

            
            Dim postBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(body)

            request.ContentLength = postBytes.Length
            Dim stream As Stream = request.GetRequestStream()
            stream.Write(postBytes, 0, postBytes.Length)
            stream.Close()

            'Get response to request.
            response = DirectCast(request.GetResponse(), HttpWebResponse)


            tempCookies.Add(response.Cookies)
            logincookiePost = tempCookies

            Console.WriteLine("Code POST : " & response.StatusCode & "  Description POST : " & response.ResponseUri.Query)

        Catch e As WebException
            'ProtocolError indicates a valid HTTP response, but with a non-200 status code (e.g. 304 Not Modified, 404 Not Found)
            If e.Status = WebExceptionStatus.ProtocolError Then
                response = DirectCast(e.Response, HttpWebResponse)
            Else
                Return False
            End If
        Catch e As Exception
            If response IsNot Nothing Then
                response.Close()
            End If
            Return False
        End Try

        Return True
    End Function



    Public Sub StopAllThread()

        Try

            threadLogin.Abort()

            threadUploadPict.Abort()

        Catch

        End Try

    End Sub

End Module
 
Code:
adTitlEncoded = WebUtility.UrlEncode(adTitl)

adTitl doesn't seem to be defined anywhere within the scope


Edit: just noticed you already solved it. Out of curiosity, what was the problem?
 
I was using the old login code with the new posting code whereas I was supposed to scrap and get the new login code in order to test the whole process with that.
 
Back
Top