本文主要是介绍java HttpGet HttpPost,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
依赖
<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;}
这篇关于java HttpGet HttpPost的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!