`
Colin_Davis
  • 浏览: 25088 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

java HttpGet HttpPost

    博客分类:
  • Java
 
阅读更多

 

    依赖

		<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;
	}

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics