Jump to content









Photo

Sending data stream to carriots from java

HTTP response 302

  • Please log in to reply
No replies to this topic

#1 tonygeorge

tonygeorge

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 13 November 2016 - 11:55 AM

Hallo,

for test purposes I'am trying to send a data stream to carriots from a java program. The problem: my java client becomes a "found 302" message. the message tells that the request has been redirected to carriots main page.

The message (HTML) is:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><html><head><title>302 Found</title></head><body><h1>Found</h1><p>The document has moved <a href="https://www.carriots.com/">here</a>.</p><hr><address>Apache/2.2.22 (Ubuntu) Server at www.carriots.com Port 80</address></body></html>

 

my java code is very direct:

 


  String url = "http://www.carriots.com:80";
  
  String APIKEY = "here my api key";  
  String DEVICE = "defaultDevice@myaccount.myaccount";
  
  URL obj = new URL(url);
  HttpURLConnection con = (HttpURLConnection) obj.openConnection();
  System.out.println("Connection ok..");
  String json = "{\"protocol\":\"v2\",\"device\":\""+DEVICE+"\",\"at\":\"now\",\"data\":{\"weight\":\"ON\"}}";
  
  HttpURLConnection.setFollowRedirects(true);
  con.setInstanceFollowRedirects(true);
  con.setUseCaches(true);
  con.setConnectTimeout(600000);   
  
  //add reuqest header
  con.setRequestMethod("POST");
  con.setRequestProperty("Host", "api.carriots.com");
  con.setRequestProperty("Accept", "application/json");
  con.setRequestProperty("User­Agent","defaultDevice@myaccount.myaccount" );
  con.setRequestProperty("Content­Type", "application/json");
  con.setRequestProperty("Content­Length", Integer.toString(json.getBytes().length));
  con.setRequestProperty("carriots.apikey", "here my api key");
  
  //con.setRequestProperty("ve", "HTTP/1.1");//some tests, with no positive results..
  //con.setRequestProperty("ur", "/streams");
 
  // Send post request
  con.setDoOutput(true);
  DataOutputStream wr = new DataOutputStream(con.getOutputStream());
  System.out.println("Writing to the DataOutputStream");
  wr.writeBytes(json);
  wr.flush();
  wr.close();
 
  int responseCode = con.getResponseCode();
  
  BufferedReader in = new BufferedReader(
          new InputStreamReader(con.getInputStream()));
  String inputLine;
  StringBuffer response = new StringBuffer();
 
  while ((inputLine = in.readLine()) != null) {
   response.append(inputLine);
  }
  in.close();
 
  //print result
  System.out.println(response.toString());
 
 }

 






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users