char server[] = "arduino-data-server.appspot.com";
Serial.println("\nStarting connection to server..."); // if you get a connection, report back via serial: if (client.connect(server, 80)) { Serial.println("connected to server"); // Make a HTTP request: client.println("POST arduino-data-server.appspot.com/dataserver HTTP/1.1"); client.println("Host: www.google.com"); client.println("Content-Length: nnn"); client.println("Connection: close"); client.println(); }}
i need to write a full http request for invoke a soap webservice hosted on app engine, but i'm not able to send request. I'm programming an arduino borad, and there's no library for invoke webservice (so i need to built it).
what code should do it's to open an http connection with webservice, then send http request with xml for call service, but if i open connection by using webservice address, nothing works.
char server[] = "http://arduino-data-server.appspot.com"; //server address
if(client.connect(server, 80)) { Serial.println("Connected to server, preparing request."); //prepare soap request client.println("POST /dataserver HTTP/1.1"); client.println("Accept: text/xml, multipart/related"); client.println("Content-Type: text/xml; charset=utf-8"); client.println("SOAPAction: \"http://example.com/Functions/SendDataRequest\""); client.println("User-Agent: unknow"); client.println("Content-Lenght: 208"); client.println("Host: arduino-data-server.appspot.com"); client.println("Connection: Keep-Alive"); client.println(); client.println("<?xml version=\"1.0\""); client.println("<S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\""); client.println("<S:Body>"); client.println("<ns2:sendData xmlsn:ns2=\"http://example.com\">"); client.println("<arg0> 33 </arg0>"); client.println("<arg1> 77% </arg1>"); client.println("</ns2:sendData>"); client.println("</S:Body>"); client.println("</S:Envelope>"); Serial.println("richiesta inviata");
}
If i've write a wrong http request, on my server log i should see an error log, but no log of arduino request appear on my log. Also if i call only connect function i should see something on log, but nothing of this happen
I've paste in previous message the arduino code take a look.
What i think is that code it's unable to connect to webservice for some dns problem
in fact also if i try to ping webservice address ( arduino-data-server.appspot.com ) the ip showed it's referred to anothe address ( appspot.l.google.com )
client.println("Content-Length:"+body.length());
client.print("Content-Length:");
client.println(body.length());
void doPost(String temperature, String humidity) { String body = HttpRequestBody(temperature, humidity); Serial.println("Preparing connection to server"); if(client.connect(server, 80)) { Serial.println("Connected to server, sending request."); //Send HTTP POST request
client.println("POST /dataserver HTTP/1.1"); client.println("Accept: text/xml, multipart/related"); client.println("Content-Type: text/xml; charset=utf-8");
client.println("SOAPAction: \"http://example.com/Functions/sendDataRequest\""); client.println("User-Agent: Arduino WiFiShield"); client.print("Content-Length: "); client.println(body.length()); client.println("Host: arduino-data-server.appspot.com"); client.println("Connection: Close"); client.println(); client.println(body); client.println(); client.stop(); Serial.println("Request sent");
String HttpRequestBody(String temp, String hum) { String res = ""; res += "<?xml version=\"1.0\"?>\n\r"; res +="<S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\">\n\r"; res +="<S:Body>\n\r"; res +="<ns2:sendData xmlns:ns2=\"http://example.com/\">\n\r"; res +="<arg0>"+temp+"</arg0>\n\r"; res +="<arg1>"+hum+"</arg1>\n\r"; res +="</ns2:sendData>\n\r"; res +="</S:Body>\n\r"; res +="</S:Envelope>"; return res;}
package com.br.service.impl;
@WebService
public class PushNotificationServiceImpl implements PushNotificationServiceLocal {
@Override
@WebMethod
public void sendPushNotification( @WebParam( name = "msg" ) String msg ) {
BasicConfigurator.configure(); try { PushNotificationPayload notificacao = PushNotificationPayload.complex(); notificacao.addAlert( msg ); notificacao.addBadge( 1 ); notificacao.addSound( "default" ); notificacao.addCustomDictionary( "id", "1" );
System.out.println( notificacao.toString() ); List<PushedNotification> NOTIFICATIONS = Push.payload( notificacao, "ck.p12", "SENHA", false, "TOKEN" );
for ( PushedNotification NOTIFICATION : NOTIFICATIONS ) { if ( NOTIFICATION.isSuccessful() ) { System.out.println( "Notificação enviada com sucesso para: " + NOTIFICATION.getDevice().getToken() );
} else { // String INVALIDTOKEN = // NOTIFICATION.getDevice().getToken();
Exception THEPROBLEM = NOTIFICATION.getException(); THEPROBLEM.printStackTrace();
ResponsePacket THEERRORRESPONSE = NOTIFICATION.getResponse(); if ( THEERRORRESPONSE != null ) { System.out.println( THEERRORRESPONSE.getMessage() ); } } }
} catch ( CommunicationException e ) { e.printStackTrace(); } catch ( KeystoreException e ) { e.printStackTrace(); } catch ( JSONException e ) { e.printStackTrace(); } } }
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char server[] = "192.168.0.28"; // meu ip
IPAddress ip(192,168,0,177); // ip para o arduino
EthernetClient client;
void setup() {
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Ethernet.begin(mac, ip);
delay(1000);
Serial.println("connecting...");
doPost("Teste notificacao push");
}
void loop()
{
if (client.available()) {
char c = client.read();
Serial.print(c);
}
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
while(true);
}
}
void doPost(String msg) {
String body = HttpRequestBody(msg);
Serial.println("Preparing connection to server");
if(client.connect(server, 80)) {
Serial.println("Connected to server, sending request.");
//Send HTTP POST request
client.println("POST /PushNotificationServiceImpl HTTP/1.1 HTTP/1.1");
client.println("Accept-Encoding: gzip,deflate");
client.println("Content-Type: text/xml; charset=UTF-8");
client.println("SOAPAction: \"http://impl.service.br.com/PushNotificationServiceImpl/sendPushNotification\""); client.println("User-Agent: arduino-ethernet");
client.print("Content-Length: ");
client.println(body.length());
client.println("Host: 192.168.0.28:8080");
client.println("Connection: Keep-Alive");
client.println();
client.println(body);
client.println();
client.stop();
Serial.println("Request sent");
Serial.println(body);
Serial.println(body.length());
}
else {
// kf you didn't get a connection to the server:
Serial.println("connection failed");
}
}
String HttpRequestBody(String msg) {
Serial.println("Montando xml");
String res = "";
res += "<?xml version=\"1.0\"?>\n\r";
res +="<S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\">\n\r";
res +="<S:Body>\n\r";
res +="<ns2:sendPushNotification xmlns:ns2=\"http://impl.service.br.com/\">\n\r";
res +="<msg>"+msg+"</msg>\n\r";
res +="</ns2:sendPushNotification>\n\r";