0

I'm quite new to Java, but I have different results when using OutputStreamWriter and DataOutputStream with a POST request of HttpUrlConnection.

I can append parameters when I use DataOutputStream.

URL url = new URL("https://www.google-analytics.com/debug/collect");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;");
conn.setRequestMethod("POST");

String parameters = "v=1&tid=UA-62749954-12&cid=test.clientid.456&t=event&ec=offline_cv&ea=register";

DataOutputStream writer = new DataOutputStream(conn.getOutputStream());
writer.writeBytes(parameters);

writer.close();
conn.connect();

Response:

{  "hitParsingResult": [ {    "valid": true,    "parserMessage": [ ],    "hit": "/debug/collect?v=1\u0026tid=UA-62749954-12\u0026cid=test.clientid.456\u0026t=event\u0026ec=offline_cv\u0026ea=register"  } ],  "parserMessage": [ {    "messageType": "INFO",    "description": "Found 1 hit in the request."  } ]}

But, I can not append parameters when using OutputStreamWriter.

OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
writer.write(URLEncoder.encode(parameters, "UTF-8"));

Response:

{  "hitParsingResult": [ {    "valid": false,    "parserMessage": [ {      "messageType": "ERROR",      "description": "A value is required for parameter 'v'. Please see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#v for details.",      "messageCode": "VALUE_REQUIRED",      "parameter": "v"    } ],    "hit": "/debug/collect"  } ],  "parserMessage": [ {    "messageType": "INFO",    "description": "Found 1 hit in the request."  } ]}

Is it because DataOutputStream is compatible with String, and OutputStreamWriter is not?

Steve
  • 29
  • 3
  • Did you bother to read the javadoc of [`DataOutputStream`](https://docs.oracle.com/javase/8/docs/api/java/io/DataOutputStream.html)? *"A data output stream lets an application write primitive Java data types to an output stream in a portable way. An application can then use a data input stream to read the data back in."* You don't need to write primitive data types in binary, so **do not use `DataOutputStream`**!! – Andreas Apr 28 '18 at 05:41
  • so, what's the alternative and what's the issue if the one is OutputStreamWriter in this case? – Steve Apr 28 '18 at 06:43
  • This is also ok. PrintStream writer = new PrintStream(conn.getOutputStream()); writer.print(parameters); writer.close(); – Steve Apr 28 '18 at 06:51

1 Answers1

1

1) Problem is here:

URLEncoder.encode(parameters, "UTF-8")

Your parameters are already url-encoded. Call of this method makes them malformed.

2) Use some tool to monitor HTTP requests, to know what occurs with request and response.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
SmInc
  • 235
  • 3
  • 11
  • It has the same result when I'm implementing like this; writer.write(parameters);. Deleted the GA tag. – Steve Apr 28 '18 at 04:06