Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
//conn.getResponseCode() returns -1 when the making multiple calls in a loop so you need to make sure that the streams are closed and the connection is fully closed
System.setProperty("http.keepAlive", "false");
java.net.URL url = new java.net.URL(context.wsdl_url);
java.net.HttpURLConnection conn = (java.net.HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("PUT");//you can specify what type of request you want to do here.... PUT, GET, POST... etc..
conn.setRequestProperty("Content-Type", "application/json");
String username = context.username;
String password = context.password;
sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
String userpassword = username + ":" + password;
String encodedAuthorization = enc.encode( userpassword.getBytes() );
conn.setRequestProperty("Authorization", "Basic "+encodedAuthorization);
conn.setAllowUserInteraction(true);
String input = "{\"order_note\": \"Order downloaded.\",\"email_note\":Something here}";//payload to send to the server...
java.io.OutputStream os = conn.getOutputStream();
os.write(input.getBytes());
os.flush();
os.close();
if (conn.getResponseCode() != java.net.HttpURLConnection.HTTP_NO_CONTENT) {
context.log += "\r\n"+currentTS+" Client:"+context.cust_code+" Order update failed. "+conn.getResponseCode()+" and message: "+conn.getResponseMessage();
throw new RuntimeException("Orders might have been downloaded but failed to update order status at store. Failed : HTTP error code : "
+ conn.getResponseCode());
}
java.io.BufferedReader br = new java.io.BufferedReader(new java.io.InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.getInputStream().close();//be sure to close the stream OR in the while loop above make sure you read ALL the contents from the stream BEFORE closing it
conn.disconnect();
System.out.println("disconnected...");