java 发送http请求(连接超时处理)

业务背景:

某项目跟第三方公司对接。

业务描述:

出于数据安全考虑,需要从服务器发送请求,来调用第三方公司提供的接口。但是该场景是销售类型,响应时间必须够快,那么就要设置响应的超时处理。

不然让客户看着圈圈在那里转半天,谁买?

项目架构:

jdk1.7

spring4.2.9

详细内容:

 

CloseableHttpClient httpclient = HttpClients.createDefault();
try {
			
	HttpPost httpPost = new HttpPost(要访问的URL);
	//配置超时
	RequestConfig requestConfig = RequestConfig.custom()    
			.setConnectTimeout(5000).setConnectionRequestTimeout(5000)    
			.setSocketTimeout(5000).build();    
	httpPost.setConfig(requestConfig);
			
	//设置post请求参数
	List nvps = new ArrayList();  
	nvps.add(new BasicNameValuePair("attr1", 参数值1));  
	nvps.add(new BasicNameValuePair("attr2", 参数值2));
	nvps.add(new BasicNameValuePair("attr3", 参数值3));
	……  
	httpPost.setEntity(new UrlEncodedFormEntity(nvps));  
			
	//执行post请求
	CloseableHttpResponse response = httpclient.execute(httpPost);
			
	// 判断网络连接状态码是否正常(0--200都数正常)
            if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
                //获取响应实体
            	responseMessage = EntityUtils.toString(response.getEntity(),"utf-8");
                System.out.println(responseMessage);
                if(responseMessage!=null && !"".equals(responseMessage)){
                	String data = new String(AESUtil.decrypt(Base64.decode(responseMessage), key),"UTF-8");
                	
         			String msg =  translate_PRE(data,trans_type,transNo);
         			result.put("msg", msg);
                }
               
            }else{
            	System.out.println("请求未成功响应: "+ response.getStatusLine());
           
            }
		
	} catch (ConnectTimeoutException e) {
		System.out.println(api_type+"请求超时");
			
	} catch (ClientProtocolException e) {
		System.out.println("请求失败");
	
	} catch (Exception e) {
		e.printStackTrace();
		
	} finally {
		//释放连接
		try {
	           	if(httpclient!=null){
	           		httpclient.close();
	           	}
	           } catch (IOException e) {
	      		System.out.println(api_type+"连接无法关闭");
	           }
	}		.setConnectTimeout(5000).setConnectionRequestTimeout(5000)    
			.setSocketTimeout(5000).build();    
	httpPost.setConfig(requestConfig);
			
	//设置post请求参数
	List nvps = new ArrayList();  
	nvps.add(new BasicNameValuePair("attr1", 参数值1));  
	nvps.add(new BasicNameValuePair("attr2", 参数值2));
	nvps.add(new BasicNameValuePair("attr3", 参数值3));
	……  
	httpPost.setEntity(new UrlEncodedFormEntity(nvps));  
			
	//执行post请求
	CloseableHttpResponse response = httpclient.execute(httpPost);
			
	// 判断网络连接状态码是否正常(0--200都数正常)
            if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
                //获取响应实体
            	responseMessage = EntityUtils.toString(response.getEntity(),"utf-8");
                System.out.println(responseMessage);
                if(responseMessage!=null && !"".equals(responseMessage)){
                	String data = new String(AESUtil.decrypt(Base64.decode(responseMessage), key),"UTF-8");
                	
         			String msg =  translate_PRE(data,trans_type,transNo);
         			result.put("msg", msg);
                }
               
            }else{
            	System.out.println("请求未成功响应: "+ response.getStatusLine());
           
            }
		
	} catch (ConnectTimeoutException e) {
		System.out.println(api_type+"请求超时");
			
	} catch (ClientProtocolException e) {
		System.out.println("请求失败");
	
	} catch (Exception e) {
		e.printStackTrace();
		
	} finally {
		//释放连接
		try {
	           	if(httpclient!=null){
	           		httpclient.close();
	           	}
	           } catch (IOException e) {
	      		System.out.println(api_type+"连接无法关闭");
	           }
	}

 

 

 

 

 

 

你可能感兴趣的:(java)