Skip to content

Instantly share code, notes, and snippets.

@tfnico
Last active September 16, 2020 10:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tfnico/4c0fbe84bfbdffae0d7a to your computer and use it in GitHub Desktop.
Save tfnico/4c0fbe84bfbdffae0d7a to your computer and use it in GitHub Desktop.
Send http request using google-http-client
// Related to https://gist.github.com/tfnico/5926374
// and http://stackoverflow.com/questions/14464486/guava-input-output-suppliers-and-urlconnection
static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
public void sendMessage(String url, String params){
final HttpURLConnection connection;
try {
HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory();
GenericUrl url = new GenericUrl(url);
HttpContent content = new ByteArrayContent("application/x-www-form-urlencoded", params.getBytes());
HttpRequest request = requestFactory.buildPostRequest(url, content);
HttpResponse response = request.execute();
System.out.println("Sent parameters: " + paramsToSend + " - Received response: " + response.getStatusMessage());
System.out.println("Response content: " + CharStreams.toString(new InputStreamReader(response.getContent())));
} catch (IOException e) {
throw Throwables.propagate(e);
}
}
@darkcrawler01
Copy link

link looping .. which one is the more modern version ?

@jayjupdhig
Copy link

link looping .. which one is the more modern version ?

That's a really good question... But there is no answser since more than 4 years, which is ridiculous...

@jayjupdhig
Copy link

Code does not work anymore with current version

@jayjupdhig
Copy link

A more modern version of https://gist.github.com/tfnico/5926374

-> That has nothing to do with the google http client library

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment