由于原始答案中的某些类已在Apache HTTP Components的较新版本中弃用,因此,我将发布此更新。
顺便说一句,你可以在此处访问完整的文档以获取更多示例。
HttpClient httpclient = HttpClients.createDefault();HttpPost httppost = new HttpPost("http://www.a-domain.com/foo/");// Request parameters and other properties.List<NamevaluePair> params = new ArrayList<NamevaluePair>(2);params.add(new BasicNamevaluePair("param-1", "12345"));params.add(new BasicNamevaluePair("param-2", "Hello!"));httppost.setEntity(new UrlEnpredFormEntity(params, "UTF-8"));//Execute and get the response.HttpResponse response = httpclient.execute(httppost);HttpEntity entity = response.getEntity();if (entity != null) { try (InputStream instream = entity.getContent()) { // do something useful }}


