Sample GET/POST Using Google AppEngine Java HttpConnection
A sample code on basic usage of HTTP GET/POST is always helpful.
I wrote once for AFNetworking (for iOS), which I subsequently referred to it myself. Hence, I am now writing for Google App Engine (Java).
Google provided documentation on http connection, but it is incomplete. Here’s my version.
GET
In my example, I want to get a JSON response for a Comment resource.
I showed how I set my custom headers, especially the Content-Type header. Then I read the response body by using BufferedReader, finally parse the string to JSONObject.
1234567891011121314151617181920212223242526
try{URLurl=newURL("https://api.myserver.com/v1/comments");HttpURLConnectionconnection=(HttpURLConnection)url.openConnection();connection.setRequestProperty("X-Custom-Header","xxx");connection.setRequestProperty("Content-Type","application/json");if(connection.getResponseCode()==HttpURLConnection.HTTP_OK){// OKBufferedReaderreader=newBufferedReader(newInputStreamReader(url.openStream()));StringBufferres=newStringBuffer();Stringline;while((line=reader.readLine())!=null){res.append(line);}reader.close();JSONObjectjsonObj=newJSONObject(res);Stringcount=jsonObj.getInt("count");...}else{// Server returned HTTP error code.}}catch(Exceptione){}
try{URLurl=newURL("https://api.myserver.com/v1/comments");HttpURLConnectionconnection=(HttpURLConnection)url.openConnection();connection.setRequestProperty("X-Custom-Header","xxx");connection.setRequestProperty("Content-Type","application/json");// POST the http body dataconnection.setDoOutput(true);connection.setRequestMethod("POST");OutputStreamWriterwriter=newOutputStreamWriter(connection.getOutputStream());writer.write("{\"comment\": \"awesome tutorial\"}");writer.close();if(connection.getResponseCode()==HttpURLConnection.HTTP_OK){// OKBufferedReaderreader=newBufferedReader(newInputStreamReader(url.openStream()));StringBufferres=newStringBuffer();Stringline;while((line=reader.readLine())!=null){res.append(line);}reader.close();JSONObjectjsonObj=newJSONObject(res);Stringcount=jsonObj.getInt("count");...}else{// Server returned HTTP error code.}}catch(Exceptione){}
Pitfalls
Google AppEngine always append to your User-Agent header, so as to identify the http request is from a particular AppEngine app. This is a bane because if you are crawling some website, they could easily block you since you can’t spoof your user-agent.