2017-10-14 67 views
0

我想使用HttpWebRequest在olx.ba上發佈廣告。要發佈廣告,我必須先登錄,然後從該請求中提取Cookie並將其傳遞給發佈廣告的請求。到目前爲止,廣告已正確發佈,但廣告沒有圖片。Httpwebrequest上傳文本和圖像

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(); 
    } 

該網站有一個形式用於上載的圖像和當我選擇要上傳的圖片,這是由該形式提出的請求:

請求頭

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 

請求有效載荷

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


------WebKitFormBoundaryAczld5sbjrh0FX5q-- 

我認爲上傳表單是來自dropzone.js,如果這有幫助的話。我怎樣才能做這項工作?

回答

1

將圖像作爲路徑使用之前。嘗試使用Convert.toBase64String方法將圖像轉換爲字節數組,然後轉換爲base64String。然後將base64字符串發佈到網站。

謝謝。

+0

但我在哪裏發送它?我把它發送到我發送其他數據的地方?廣告詳細信息必須轉到https://www.olx.ba/objava/zavrsi,圖像轉到https://www.olx.ba/objava/upload。你能給我一個例子嗎?謝謝! – qpBlaze

+0

以formdata發送2.jpg圖像的數據URL。如果一切都很好,那麼困擾服務器的唯一可能是** Content-Type **。嘗試設置** Content-Type **:multipart/form-data;而不是image/jpeg。 –