-->
Page 1 of 1

HttpClient Post (-1 (error: connection refused))

PostPosted: Tue May 30, 2017 1:37 pm
by Ruandv
I have posted this question also on Stack overflow but I hope that i will get a faster answer here ;-)

I am trying to create a call to a server on the web making use of HttpPost but i keep getting a response of -1 (error: connection refused)

I am able to make an HTTPGet request but my httpPost fails. If i take that same data that i am trying to post and put that into Fiddler and do a post from there i get a 200 OK.

Below is the code that i am trying to execute :

void PostData() {
String data = "mailSend=1&new=1&rec=Gupta+Classic&topic=ddd&reply=ddd&fbSig=bE9";
String url = "https://www.shippingmanager.dk/sh/mailbox.php";
HTTPClient http;
http.setReuse(true);
http.begin(url);
http.addHeader("Host", "www.shippingmanager.dk");
http.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0");
http.addHeader("Accept-Language", " en-US,en;q=0.5");
http.addHeader("Accept-Encoding", " gzip, deflate, br");
http.addHeader("Content-Type", " application/x-www-form-urlencoded; charset=UTF-8");
http.addHeader("X-Requested-With", " XMLHttpRequest");
http.addHeader("Referer", " https://www.shippingmanager.dk/sh/?fb_s ... manager.dk");
http.addHeader("Connection", "keep-alive");
http.addHeader("Pragma", " no-cache");
http.addHeader("Cache-Control", " no-cache");

int result = http.POST(data);
http.writeToStream(&Serial);
http.end();

}

this is the raw text that i post in Fiddler to do the post :

POST https://www.shippingmanager.dk/sh/mailbox.php HTTP/1.1
Host: www.shippingmanager.dk
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0
Accept: */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: https://www.shippingmanager.dk/sh/?fb_s ... manager.dk
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache

mailSend=1&new=1&rec=Gupta+Classic&topic=ddd&reply=ddd&fbSig=J9

Please can someone point me to my problem.

I also tried the code in this link and that also failed me :-(

viewtopic.php?f=29&t=14054

Re: HttpClient Post (-1 (error: connection refused))

PostPosted: Tue May 30, 2017 7:39 pm
by QuickFix
Just a shot in the dark: what headers are already set before you add yours?
Perhaps you first have to clear the default headers when adding your own to prevent doubles?

Do some more debugging by setting up a HTTP-server with PHP installed and call this page from your ESP:
Code: Select all<?php
  $text='<html><head><title>PHP Environment</title></head><body>'.
        '<table style="border-collapse: collapse;" cellpadding="1">';
  foreach ($_SERVER as $key => $val)
    $text.='<tr onMouseOver="this.style.backgroundColor=\'AAAABB\';" '.
          'onMouseOut="this.style.backgroundColor=\'transparent\';">'.
         '<td style="font-weight: bold; border-right: 2px solid #000000;">'.
         $key.'</td><td style="width: 100%;">'.
         (is_array($val)?nl2br(print_r($val,true)):$val).'</td></tr>';
  $text.='</table>';
  echo $text.'</body></html>';
?>

You might want to log the result on the server, instead of sending it back to the ESP.

Re: HttpClient Post (-1 (error: connection refused))

PostPosted: Tue May 30, 2017 11:04 pm
by Ruandv
Unfortunately I don't have access to the server. It's a game that is hosted and I want to skim data from the page (which is working fine) with my http Get. And then after that perform the Http Post as listed above. :?

Re: HttpClient Post (-1 (error: connection refused))

PostPosted: Wed May 31, 2017 3:35 am
by gbafamily1
Every server using TLS/SSL has a different certificate and each certificate has a different SHA1 hash. Be sure to use the correct hash for the server you are accessing.