ursachan
ursachan

Reputation: 194

Httpwebrequest upload text and images

I want to post an ad on olx.ba using HttpWebRequest. To post an ad I had to log in first, so I took the cookies from that request and passed it to the request that posts the ad. Until now the ad is posted correctly, but the ad has no images.

public void PostProduct(Account account, Product product)
    {
        // Get login cookies
        CookieCollection cookieCollection = GetLoginCookies(account);

        request = InitializeRequest(request, "https://www.olx.ba/objava/zavrsi");

        // Set the login cookies
        foreach (Cookie c in response.Cookies)
        {
            request.CookieContainer.Add(c);
        }

        postData = Encoding.ASCII.GetBytes(UrlHelpers.ToQueryString(product));
        request.ContentLength = postData.Length;

        using (var stream = request.GetRequestStream())
        {
            stream.Write(postData, 0, postData.Length);
        }

        response = (HttpWebResponse)request.GetResponse();

        // responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
    }

The website has a form for uploading the images and when i select an image to upload this is the request made by that form:

Request headers

POST /objava/upload?s=RBDQpWEcUu HTTP/1.1
Host: www.olx.ba
Connection: keep-alive
Content-Length: 283041
Pragma: no-cache
Cache-Control: no-cache
Origin: https://www.olx.ba
X-File-Name: 2.jpg
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36
Content-Type: multipart/form-data; boundary=---WebKitFormBoundaryAczld5sbjrh0FX5q
Accept: application/json
X-Requested-With: XMLHttpRequest
Referer: https://www.olx.ba/objava
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.8,ro;q=0.6,de;q=0.4
Cookie: xxxxx

Request Payload

------WebKitFormBoundaryAczld5sbjrh0FX5q
Content-Disposition: form-data; name="myfile"; filename="2.jpg"
Content-Type: image/jpeg


------WebKitFormBoundaryAczld5sbjrh0FX5q--

I think the upload form is from dropzone.js if that helps in any way. How can i make this work?

Upvotes: 0

Views: 596

Answers (1)

Amaid Niazi
Amaid Niazi

Reputation: 162

Before uoloading images as a path. Try to convert the images to byte array and then to the base64String using Convert.toBase64String method. Then post the base64 string to the website.

Thanks.

Upvotes: 1

Related Questions