Last active
September 16, 2020 10:12
-
-
Save tfnico/4c0fbe84bfbdffae0d7a to your computer and use it in GitHub Desktop.
Send http request using google-http-client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} | |
} |
link looping .. which one is the more modern version ?
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...
Code does not work anymore with current version
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
A more modern version of https://gist.github.com/tfnico/5926374
See https://github.com/google/google-http-java-client for more info