依赖
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.3.3</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpmime</artifactId> <version>4.3.3</version> <scope>runtime</scope> </dependency>
HttpGet
public String getVersion() { String url = "http://***********"; HttpGet httpGet = new HttpGet( url ); HttpClient httpClient = HttpClients.createDefault(); try { HttpResponse resposne = httpClient.execute( httpGet ); int statusCode = resposne.getStatusLine().getStatusCode(); if ( statusCode == 200 ) { InputStream input = resposne.getEntity().getContent(); byte[] buf = new byte[ input.available() ]; input.read( buf ); String result = new String( buf ); url = url + " ---> ############## ---> " + result; } else { } } catch ( Exception e ) { e.printStackTrace(); } return url; }
HttpPost
public String doPost( String param1, String param2, String param3 ) { String url = "http://***********"; HttpPost httpPost = new HttpPost( url ); List< NameValuePair > values = new ArrayList< NameValuePair >(); { values.add( new BasicNameValuePair( "param1", param1 ) ); values.add( new BasicNameValuePair( "param2", param2 ) ); values.add( new BasicNameValuePair( "param3", param3 ) ); } try { httpPost.setEntity( new UrlEncodedFormEntity( values, "UTF-8" ) ); HttpClient httpClient = HttpClients.createDefault(); HttpResponse resposne = httpClient.execute( httpPost ); int statusCode = resposne.getStatusLine().getStatusCode(); if ( statusCode == 200 ) { InputStream input = resposne.getEntity().getContent(); byte[] buf = new byte[ input.available() ]; input.read( buf ); String result = new String( buf ); url = url + " ---> ############## ---> " + result; } else { } } catch ( Exception e ) { e.printStackTrace(); } return url; }