Java.net

Apache HttpClient

<dependency>
     <groupId>org.apache.httpcomponents</groupId>
     <artifactId>httpclient</artifactId>
     <version>4.3.4</version>
 </dependency>
//Инициализация клиента
String USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) " +
            "AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.151 Safari/535.19";
HttpClientBuilder clientBuilder;
clientBuilder = HttpClients.custom().setUserAgent(USER_AGENT);
CloseableHttpClient httpClient = clientBuilder.build();
 
//Get запрос
HttpGet httpGet = new HttpGet(requestURLWithParams);
CloseableHttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
String responseText = EntityUtils.toString(entity, "UTF-8");
 
//POST запрос
List<NameValuePair> postMessage = new ArrayList<>();
postMessage.add(new BasicNameValuePair("test", "Hello World"));
String requestURL = "http://site.com/addWord";
HttpPost httpPost = new HttpPost(requestURL);
httpPost.setEntity((new UrlEncodedFormEntity(postMessage, Consts.UTF_8)));
CloseableHttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
String responseText = EntityUtils.toString(entity, "UTF-8");
Пока не указано иное, содержимое этой страницы распространяется по лицензии Creative Commons Attribution-ShareAlike 3.0 License