Go Down

Topic: POST HTTP SENDER (Read 663 times) previous topic - next topic

Giovax

Feb 23, 2018, 06:16 pm Last Edit: Feb 23, 2018, 11:30 pm by Giovax
Good. I really do this for the last option.
I have already tried several days to send a successful post petition to a web service.
I did it with the WCF client, create an application in c # that consumed it and everything works fine. Except with arduino. This is the shipping code, using arduino UNO and Ethernet shield2

Code: [Select]
 Serial.println("In SOPA");
    Serial.println("Connected to Server");
    client.println(F("POST /WS_PLANILLA.Service1.svc?wsdl HTTP/1.1"));
    client.println(F("Host:192.168.0.101:8312"));
    client.println(F("Content-Type:text/xml; charset=utf-8"));
    client.println(F("Content-Length:215"));
    client.println();  
    client.print(F("<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">"));
    client.print(F("<s:Header>"));

    client.print(F("<Action s:mustUnderstand=\"1\" xmlns=\"http://schemas.microsoft.com/ws/2005/05/addressing/none\">http://tempuri.org/IService1/ConsultaID</Action>"));
    client.print(F("</s:Header>"));
    client.print(F("<s:Body>"));
    client.print(F("<ConsultaID xmlns=\"http://tempuri.org/\"/>"));
    client.print(F("</s:Body>"));    
    client.print(F("</s:Envelope>"));      
    client.println();
    Serial.println("MAKE REQUEST DONE");[/sup]



and the response:


[sup]connecting...
192.168.0.103
In SOPA
Connected to Server
MAKE REQUEST DONE
HTTP/1.1 400 Bad Request
Server: Microsoft-IIS/10.0
X-Powered-By: ASP.NET
Date: Fri, 23 Feb 2018 17:10:32 GMT
Content-Length: 0

HTTP/1.1 400 Bad Request
Content-Type: text/html; charset=us-ascii
Server: Microsoft-HTTPAPI/2.0
Date: Fri, 23 Feb 2018 17:10:32 GMT
Connection: close
Content-Length: 326

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Bad Request</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Bad Request - Invalid Verb</h2>
<hr><p>HTTP Error 400. The request verb is invalid.</p>
</BODY></HTML>

disconnecting.


Any help is welcome

christop

If I'm counting it right, your request body is longer than 215 bytes. It's more like 299 bytes.

Giovax


the number of the request must be exact ??

and thanks for the answer

Giovax

#3
Feb 23, 2018, 10:02 pm Last Edit: Feb 23, 2018, 11:31 pm by Giovax
Here I have new information. I put the correct amount. That message I was sending was the one generated by the WCF client, change it by the message generated by SOAPUI, which is what I use in a C # application that I did to test this.

It is the following:

Code: [Select]
 Serial.println("In SOPA");
   Serial.println("Connected to Server");
   client.println(F("POST /WS_PLANILLA.Service1.svc?wsdl HTTP/1.1"));
   client.println(F("Host:192.168.0.101:8312"));
   client.println(F("Content-Type:text/xml; charset=utf-8"));
   client.println(F("Content-Length:201"));
   client.println();  
   client.print(F("<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:tem=""http://tempuri.org/"">"));
   client.print(F("<soapenv:Header/>"));

   client.print(F("<soapenv:Body>"));
   client.print(F("<tem:ConsultaID/>"));
   client.print(F("</soapenv:Body>"));
   client.print(F("</soapenv:Envelope>"));  
   client.println();
   Serial.println("MAKE REQUEST DONE");"



The problem is that now I do not receive any response, as if it was not sent.


"connecting...
192.168.0.103
In SOPA
Connected to Server
MAKE REQUEST DONE

disconnecting.

PieterP

The problem is that now I do not receive any response, as if it was not sent.
Of course not, you don't have any code that would read or show the response.

Pieter

Giovax

#5
Feb 23, 2018, 11:21 pm Last Edit: Feb 23, 2018, 11:30 pm by Giovax
Sorry. The misunderstanding is because I have only posted the POST shipping code, here we show full


Code: [Select]

#include <SPI.h>
#include <Ethernet2.h>


byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

byte server[] = { 192, 168, 0, 101 };

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 177);

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
  ; // wait for serial port to connect. Needed for native USB port only
}

// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
  Serial.println("Failed to configure Ethernet using DHCP");
  // try to congifure using IP address instead of DHCP:
  Ethernet.begin(mac, ip);
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");
Serial.println(Ethernet.localIP());

// if you get a connection, report back via serial:

if (client.connect(server, 8312)) {
      soapEnvelope();
} else {
  // if you didn't get a connection to the server:
  Serial.println("connection failed");
}
}



String soapEnvelope()
{
Serial.println("In SOPA");
  Serial.println("Connected to Server");
  client.println(F("POST /WS_PLANILLA.Service1.svc?wsdl HTTP/1.1"));
  client.println(F("Host:192.168.0.101:8312"));
  client.println(F("Content-Type:text/xml; charset=utf-8"));
  client.println(F("Content-Length:204"));
  client.println();  
  client.print(F("<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:tem=""http://tempuri.org/"">"));
  client.print(F("<soapenv:Header/>"));

  client.print(F("<soapenv:Body>"));
  client.print(F("<tem:ConsultaID/>"));
  client.print(F("</soapenv:Body>"));
  client.print(F("</soapenv:Envelope>"));  
  client.println();
  Serial.println("MAKE REQUEST DONE");

}

void loop() {
// if there are incoming bytes available
// from the server, read them and print them:
if (client.available()) {
  char c = client.read();
  Serial.print(c);
}

// if the server's disconnected, stop the client:
if (!client.connected()) {
  Serial.println();
  Serial.println("disconnecting.");
  client.stop();

  // do nothing forevermore:
  while (true);
}
}

PieterP

There's no guarantee that there's data available to read when entering the loop.

Code:
void loop() {
  while (client.connected()) {
    // if there are incoming bytes available
    // from the server, read them and print them:
    if (client.available()) {
      char c = client.read();
      Serial.print(c);
    }
  }
  Serial.println();
  Serial.println("disconnecting.");
  client.stop();

  // do nothing forevermore:
  while (true);
}

You'll probably have to add a "Connection: close" header in your request.

Giovax

I am very thankfull for your help.

I added the closing tag, in fact I had already done it in other tests, only that they were so many that I had forgotten.

But I still do not get an answer.

P.D: How do you make the color code?

Code: [Select]



#include <SPI.h>
#include <Ethernet2.h>


byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

byte server[] = { 192, 168, 0, 101 };

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 177);

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");
  Serial.println(Ethernet.localIP());
  
  // if you get a connection, report back via serial:
  
  if (client.connect(server, 8312)) {
        soapEnvelope();
  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}



String soapEnvelope()
{
  Serial.println("In SOPA");
    Serial.println("Connected to Server");
    client.println(F("POST /WS_PLANILLA.Service1.svc?wsdl HTTP/1.1"));
    client.println(F("Host:192.168.0.101:8312"));
    client.println(F("Content-Type:text/xml; charset=utf-8"));
    client.println(F("Content-Length:204"));
    client.println(F("Connection: close"));
    client.println();  
    client.print(F("<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:tem=""http://tempuri.org/"">"));
    client.print(F("<soapenv:Header/>"));

    client.print(F("<soapenv:Body>"));
    client.print(F("<tem:ConsultaID/>"));
    client.print(F("</soapenv:Body>"));
    client.print(F("</soapenv:Envelope>"));  

    client.println();
    Serial.println("MAKE REQUEST DONE");
  
}

void loop() {
  // if there are incoming bytes available
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    while (true);
  }
}

Giovax


Well I have published the same WS, which only has one method that returns "4" in the following address. In case you can help something.

http://52.36.59.165:8312/WS_PLANILLA.Service1.svc?wsdl

PieterP


Giovax

#10
Feb 24, 2018, 01:19 am Last Edit: Feb 24, 2018, 02:30 am by Giovax
Yes.Iam Sorry, that I uploaded the wrong code.


Code: [Select]


#include <SPI.h>
#include <Ethernet2.h>


byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

byte server[] = { 192, 168, 0, 101 };

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 177);

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");
  Serial.println(Ethernet.localIP());
  
  // if you get a connection, report back via serial:
  
  if (client.connect(server, 8312)) {
        soapEnvelope();
  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}



String soapEnvelope()
{
  Serial.println("In SOPA");
    Serial.println("Connected to Server");
    client.println(F("POST /WS_PLANILLA.Service1.svc HTTP/1.1"));
    client.println(F("Host:192.168.0.101:8312"));
    client.println(F("Content-Type:text/xml; charset=utf-8"));
    client.println(F("Content-Length:204"));
    client.println(F("Connection: close"));
    client.println();  
    client.print(F("<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:tem=""http://tempuri.org/"">"));
    client.print(F("<soapenv:Header/>"));

    client.print(F("<soapenv:Body>"));
    client.print(F("<tem:ConsultaID/>"));
    client.print(F("</soapenv:Body>"));
    client.print(F("</soapenv:Envelope>"));  

    client.println();
    Serial.println("MAKE REQUEST DONE");
  
}

void loop() {
  while (client.connected()) {
    // if there are incoming bytes available
    // from the server, read them and print them:
    if (client.available()) {
      char c = client.read();
      Serial.print(c);
    }
  }
  Serial.println();
  Serial.println("disconnecting.");
  client.stop();

  // do nothing forevermore:
  while (true);
}


But i got the same result

Giovax


Look, I do not understand this. If I change the connection tag I receive the following error:

CODE:

Code: [Select]
#include <SPI.h>
#include <Ethernet2.h>


byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

byte server[] = { 192, 168, 0, 101 };

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 177);

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");
  Serial.println(Ethernet.localIP());
 
  // if you get a connection, report back via serial:
 
  if (client.connect(server, 8312)) {
        soapEnvelope();
  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}



String soapEnvelope()
{
  Serial.println("In SOPA");
    Serial.println("Connected to Server");
    client.println(F("POST /WS_PLANILLA.Service1.svc HTTP/1.1"));
    client.println(F("Host:192.168.0.101:8312"));
    client.println(F("Content-Type:text/xml; charset=utf-8"));
    client.println(F("Content-Length:204"));
   
    client.println("");
    client.print(F("<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:tem=""http://tempuri.org/"">")); 
    client.print(F("<soapenv:Header/>"));   
    client.print(F("<soapenv:Body>"));
    client.print(F("<tem:ConsultaID/>"));
    client.print(F("</soapenv:Body>"));
    client.print(F("</soapenv:Envelope>"));
    client.println(F("Connection: close"));
        client.println("");
    Serial.println("MAKE REQUEST DONE");
 
}

void loop() {
  while (client.connected()) {
    // if there are incoming bytes available
    // from the server, read them and print them:
    if (client.available()) {
      char c = client.read();
      Serial.print(c);
    }
  }
  Serial.println();
  Serial.println("disconnecting.");
  client.stop();

  // do nothing forevermore:
  while (true);
}


ANSWER:

connecting...
192.168.0.103
In SOPA
Connected to Server
MAKE REQUEST DONE
HTTP/1.1 400 Bad Request
Server: Microsoft-IIS/10.0
X-Powered-By: ASP.NET
Date: Mon, 26 Feb 2018 15:02:23 GMT
Content-Length: 0




And that's fine, the request is wrong, but if I put the connection tag where it should be, I simply do not receive anything.

CODE

Code: [Select]
#include <SPI.h>
#include <Ethernet2.h>


byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

byte server[] = { 192, 168, 0, 101 };

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 177);

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");
  Serial.println(Ethernet.localIP());
 
  // if you get a connection, report back via serial:
 
  if (client.connect(server, 8312)) {
        soapEnvelope();
  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}



String soapEnvelope()
{
  Serial.println("In SOPA");
    Serial.println("Connected to Server");
    client.println(F("POST /WS_PLANILLA.Service1.svc HTTP/1.1"));
    client.println(F("Host:192.168.0.101:8312"));
    client.println(F("Content-Type:text/xml; charset=utf-8"));
    client.println(F("Content-Length:204"));
    client.println(F("Connection: close"));
    client.println("");
    client.print(F("<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:tem=""http://tempuri.org/"">")); 
    client.print(F("<soapenv:Header/>"));   
    client.print(F("<soapenv:Body>"));
    client.print(F("<tem:ConsultaID/>"));
    client.print(F("</soapenv:Body>"));
    client.print(F("</soapenv:Envelope>"));
        client.println("");
    Serial.println("MAKE REQUEST DONE");
 
}

void loop() {
  while (client.connected()) {
    // if there are incoming bytes available
    // from the server, read them and print them:
    if (client.available()) {
      char c = client.read();
      Serial.print(c);
    }
  }
  Serial.println();
  Serial.println("disconnecting.");
  client.stop();

  // do nothing forevermore:
  while (true);
}


ANSWER:

connecting...
192.168.0.103
In SOPA
Connected to Server
MAKE REQUEST DONE

PieterP

That's very strange ... I'm afraid I can't help you. Did you try sending the request using cURL or netcat?

Giovax


To test if the Ws works ???. I already tested it with WCF client and a small application in c # I did.


But doing more tests I have another interesting example. look at this code

Code: [Select]
#include <SPI.h>
#include <Ethernet2.h>


byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

byte server[] = { 192, 168, 0, 101 };

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 177);

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");
  Serial.println(Ethernet.localIP());
 
  // if you get a connection, report back via serial:
 
  if (client.connect(server, 8312)) {
        soapEnvelope();
  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}



String soapEnvelope()
{
  Serial.println("In SOPA");
    Serial.println("Connected to Server");
    client.println(F("POST /WS_PLANILLA.Service1.svc HTTP/1.1"));
    client.println(F("SOAPAction: http://tempuri.org/IService1/ConsultaID"));     
    client.println(F("Content-Type:text/xml; charset=utf-8"));
    client.println(F("Accept: text/xml"));
    client.println(F("Host:192.168.0.101:8312"));
    client.println(F("Content-Length: 212"));
    client.println(F("Expect: 100-continue"));
    client.println(F("Connection: close"));
 
    client.print(F("<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:tem=""http://tempuri.org/"">")); 
    client.print(F("<soapenv:Header/>"));   
    client.print(F("<soapenv:Body>"));
    client.print(F("<tem:ConsultaID/>"));
    client.print(F("</soapenv:Body>"));
    client.print(F("</soapenv:Envelope>"));
    client.println("");
    Serial.println("MAKE REQUEST DONE");
 
}

void loop() {
  while (client.connected()) {
    // if there are incoming bytes available
    // from the server, read them and print them:
    if (client.available()) {
      char c = client.read();
      Serial.print(c);
    }
  }
  Serial.println();
  Serial.println("disconnecting.");
  client.stop();

  // do nothing forevermore:
  while (true);
}




missing the line break after the header. And I get this error:

connecting...
192.168.0.103
In SOPA
Connected to Server
MAKE REQUEST DONE
HTTP/1.1 400 Bad Request
Content-Type: text/html; charset=us-ascii
Server: Microsoft-HTTPAPI/2.0
Date: Mon, 26 Feb 2018 17:18:51 GMT
Connection: close
Content-Length: 339

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Bad Request</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Bad Request - Invalid Header</h2>
<hr><p>HTTP Error 400. The request has an invalid header name.</p>
</BODY></HTML>

disconnecting.



But if I put it in, again I do not get anything


connecting...
192.168.0.103
In SOPA
Connected to Server
MAKE REQUEST DONE



 :o  :o  :o  :o


Thanks for all the help

Giovax

#14
Feb 26, 2018, 06:45 pm Last Edit: Feb 26, 2018, 06:51 pm by Giovax
more data, look what happens if I remove the label
  client.println(F("Content-Length: 212")); :


connecting...
192.168.0.103
In SOPA
Connected to Server
MAKE REQUEST DONE
HTTP/1.1 411 Length Required
Content-Type: text/html; charset=us-ascii
Server: Microsoft-HTTPAPI/2.0
Date: Mon, 26 Feb 2018 17:43:38 GMT
Connection: close
Content-Length: 344

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Length Required</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Length Required</h2>
<hr><p>HTTP Error 411. The request must be chunked or have a content length.</p>
</BODY></HTML>

disconnecting.


The problem is obvious. I just do not understand why if you put the label it just does not respond

Go Up