Use HttpClient to call the interface in the background

//The interface is bound to the condition of the object and returns the json data
public static String executeHttpRequestByGetType(String url,Object obj ) {  
	HttpClient client = new HttpClient();  
	StringBuffer sb = new StringBuffer(url);  
	Map<String, String> keyValueMap=objectToMap(obj);
	PostMethod postMethod = null;  
	try {  
	    //set request parameters  
	    if (keyValueMap != null) {  
	        Iterator it = keyValueMap.entrySet().iterator();  
	        if (keyValueMap.size() > 0) {  
	            sb.append("?");  
	             while (it.hasNext()) {  
	                  Map.Entry<String, String> entry = (Map.Entry<String, String>) it.next();  
	                  sb.append(entry.getKey() + "=" + entry.getValue() + "&");  
	              }  
	             sb.deleteCharAt(sb.length()-1);  
	         }  
	     }  
	     postMethod = new PostMethod(sb.toString());  
	     //log.debug("query uri ===============" + postMethod.getURI());
	     // The default recovery strategy is set, and it will automatically retry 3 times when an exception occurs.
	     postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());  
	     //Set the parameter encoding to gbk
	     postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");  
	     //set timeout  
	     postMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 200000);
	     //The executeMethod method of the instance created in the first step of HttpClient executes the method instance created in the second step
	     int statusCode = client.executeMethod(postMethod);  
	     if (statusCode != HttpStatus.SC_OK) {  
	         //log.info("request '" + url + "' failed,the status is not 200,status:" + statusCode);  
	         return "";  
	     }  
	     String responseBody = postMethod.getResponseBodyAsString();  
	     return responseBody;  
	     } catch (Exception e) {  
	            //log.error("Exception occurred! Please check network and parameters", e);  
	      } finally {  
	            if(postMethod!=null){  
	               postMethod.releaseConnection();  
	            }  
	        }  
	      return null;  
       }
@Test
public void test(){
	TestBean be=new TestBean();
	InterfaceData da=new InterfaceData();
	String url= "http://59.111.44.97:9096/webapi/TakeAll";//Interface address
	Map<String, String> msp=new HashMap<String, String>();
	String s=da.executeHttpRequestByGetType(url,be);
	//JSONUtil.deserialize(s);
	System.out.println(s);
 }
	 
//object is converted to Map
public static Map<String,String> objectToMap(Object obj){
        Map<String,String> map=null;
        if(obj==null){
	   return map;
	}else{
	   try {
		map= new HashMap<String,String>();
		BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
		PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
		for (PropertyDescriptor property : propertyDescriptors) {    
		      String key = property.getName();    
		      if (key.compareToIgnoreCase("class") == 0) {   
		          continue;  
		      }  
		      Method getter = property.getReadMethod();  
		      Object value = getter!=null ? getter.invoke(obj) : null;
		      if(value!=null){
		       	map.put(key, value.toString());  
		       }
		   }    
		  return map;  
		  } catch (IntrospectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace ();
		  }catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace ();
		  } catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace ();
		  } catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace ();
		  }
		return null;
	     }
     }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326162740&siteId=291194637