Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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);
}
}
@tfnico
Copy link
Author

tfnico commented Jul 1, 2015

@darkcrawler01
Copy link

darkcrawler01 commented Feb 24, 2016

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

@jayjupdhig
Copy link

jayjupdhig commented Sep 16, 2020

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

jayjupdhig commented Sep 16, 2020

Code does not work anymore with current version

@jayjupdhig
Copy link

jayjupdhig commented Sep 16, 2020

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