EDIT: Now that I've figured out some behavior, I'm wondering, why does it not send the request as soon as I call flush()
? or close()
? Why does it wait until I call getResponseCode()
?
Original question:
I am new to the HttpsURLConnection class in java, and looked on the doc and found it not very enlightening. I have inherited this code on a project I am working on (I did not write it myself) and wondered what it's doing, and how it can be improved. For example, I can't tell if "writeBytes" actually sends the data or "close." I've looked at many javadocs and found them to be ambiguous, and haven't found any good resources for this topic in books or blog posts online. Could someone please enlighten me, or point me to some good resources?
By the way, this code is for an android SDK library I am working on.
Note: I understand the theory of HTTP requests very well. I took a class on it. I know all about URL parameters (using the ?name=value) and cookies and RESTful services and TCP and IP... etc. Just struggling to find good docs so I know how to use the java libraries.
Edit: Changed this to the HttpClient code because it turns out I couldn't use Https and still see the request from the echo server. It has the same idea though.
import java.io.BufferedInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class HttpClient
{
public static final String DEBUG_URL = "http://10.20.1.61:8001/api/ad/v5/";
public static final String MAPPED_KEYWORDS = "get_mapped_keywords/";
private static byte[] buffer = new byte[1024];
static NetworkReturn sendHttpPost(String urlString, String postData)
{
URL url;
HttpURLConnection con = null;
try {
url = new URL(urlString);
con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-length", String.valueOf(postData.length()));
con.setRequestProperty("Content-Type", "application/json");
con.setDoOutput(true);
DataOutputStream output = new DataOutputStream(con.getOutputStream());
output.writeBytes(postData);
output.close();
StringBuilder result = new StringBuilder(), error = new StringBuilder();
int responseCode = con.getResponseCode();
if (responseCode >= 200 && responseCode < 300)
{
if (con.getInputStream() != null)
{
InputStream in = new BufferedInputStream(con.getInputStream());
int length;
while ((length = in.read(buffer)) != -1)
{
result.append(new String(buffer, 0, length));
}
}
}
else
{
if (con.getErrorStream() != null)
{
InputStream in = new BufferedInputStream(con.getErrorStream());
int length;
while ((length = in.read(buffer)) != -1)
{
error.append(new String(buffer, 0, length));
}
}
}
return new NetworkReturn(responseCode, result.toString(), error.toString());
}
catch (MalformedURLException e) {
return new NetworkReturn(-1, "", "MalformedURLException: " + e.getLocalizedMessage());
}
catch (IOException e) {
e.printStackTrace();
return new NetworkReturn(-1, "", "IOEXCEPTION: " + e.getLocalizedMessage());
}
finally {
if (con != null)
con.disconnect();
}
}
}