I am working with java and I am sending data to a PHP page. The goal is to send and store an object and then retrieve that object at a later time. As for now I am simply sending data and then having it echoed back. The response isn't quite what I send. First off, the response always starts with an ascii 32 (or space character). After that, sending an integer works up to 127. After that not only is the response wrong, it adds bytes.
Here is a response using 127: (the first line is the value out, the second line is the response back)
[0, 0, 0, 127]
[32, 0, 0, 0, 127]: 127
Now with 128:
[0, 0, 0, -128]
[32, 0, 0, 0, -17, -65, -67]: 239
I am at a loss and am not sure what I am doing wrong. I think it is an encoding issue involving signed / unsigned numbers but am not sure, especially since bytes are being added in from the response. I have successfully retrieved data using a responseStreamReader for String data but my data is getting more complicated and includes characters that split the String causing issues. Below is my code:
First for the PHP, very simple retrieve-respond:
<?
$data = file_get_contents('php://input');
echo $data;
?>
and now for my java code:
public static void makeConnection(String urlPath, ByteArrayOutputStream outStream, ByteArrayOutputStream inStream) throws IOException {
HttpURLConnection con = null;
URL url = new URL(urlPath);
con = (HttpURLConnection) url.openConnection();
con.setUseCaches(false);
con.setDoOutput(true);
con.setRequestMethod("POST");
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Cache-Control", "no-cache");
con.setRequestProperty("Content-Type", "Content-Type: application/octet-stream");
DataOutputStream request = new DataOutputStream(con.getOutputStream());
System.out.println(Arrays.toString(outStream.toByteArray())); //what the byte array looks like going out
request.write(outStream.toByteArray());
request.flush();
request.close();
DataInputStream resp = new DataInputStream(con.getInputStream());
DataOutputStream rosp = new DataOutputStream(inStream);
byte[] b = new byte[1024];
int len;
while((len = resp.read(b, 0, 1024)) > 0){
rosp.write(b, 0, len);
}
rosp.close();
resp.close();
con.disconnect();
}
public static void main(String args[]) {
try {
ByteArrayOutputStream bs = new ByteArrayOutputStream();
DataOutputStream request = new DataOutputStream(bs);
ByteArrayOutputStream is = new ByteArrayOutputStream();
request.writeInt(128); //127 works, this does not
makeConnection(urlWip, bs, is);
byte resp[] = new byte[is.size()];
resp = is.toByteArray();
request.flush();
request.close();
bs.flush();
bs.close();
is.flush();
is.close();
ByteArrayInputStream bis = new ByteArrayInputStream(resp);
DataInputStream response = new DataInputStream(bis);
response.readByte(); //There is a space padding response. Why?
int test = response.readInt(); //Here is where our response should be
response.close();
bis.close();
System.out.println(Arrays.toString(resp) + ": " + test);
} catch (MalformedURLException | SocketException e) {
System.out.println("url error"); // Connection Error. Check Internet and Try Again
} catch (IOException e) {
System.out.println("data error"); //Corrupted File Error
}
}