如何用java模拟ajax数据发送请求

jerry Java 2015年08月10日 收藏

案例1:

  1. import org.apache.commons.httpclient.*;  
  2. import org.apache.commons.httpclient.methods.*;  
  3. import org.apache.commons.httpclient.params.HttpMethodParams;  
  4.   
  5. import java.io.*;  
  6.   
  7. public class HttpClientTutorial {  
  8.     
  9.   private static String url = "http://10.129.39.149:8090/ajax/loginMgt/login.action";  
  10.   
  11.   public static void method(HttpClient client,String url,String body){  
  12.       PostMethod  method = new PostMethod(url);  
  13.         //"count":10,"ignoreCase":"false","paras":["a%"],"queryId":"getMenu"  
  14.         NameValuePair[] postData = new NameValuePair[]{};    
  15.         //postData[0] = new NameValuePair("count", 10);   
  16.         method.setRequestBody(body);//addParameters(postData);   
  17.           
  18.           
  19.         // Provide custom retry handler is necessary  
  20.         /*method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,  
  21.                 new DefaultHttpMethodRetryHandler(3, false));*/  
  22.   
  23.         try {  
  24.           // Execute the method.  
  25.           int statusCode = client.executeMethod(method);  
  26.   
  27.           if (statusCode != HttpStatus.SC_OK) {  
  28.             System.err.println("Method failed: " + method.getStatusLine());  
  29.           }  
  30.   
  31.           // Read the response body.  
  32.           byte[] responseBody = method.getResponseBody();  
  33.   
  34.           // Deal with the response.  
  35.           // Use caution: ensure correct character encoding and is not binary data  
  36.           System.out.println(new String(responseBody,"utf-8"));  
  37.         } catch (HttpException e) {  
  38.             System.err.println("Fatal protocol violation: " + e.getMessage());  
  39.             e.printStackTrace();  
  40.           } catch (IOException e) {  
  41.             System.err.println("Fatal transport error: " + e.getMessage());  
  42.             e.printStackTrace();  
  43.           } finally {  
  44.             // Release the connection.  
  45.             method.releaseConnection();  
  46.           }    
  47.   }  
  48.     
  49.   public static void main(String[] args) {  
  50.     // Create an instance of HttpClient.  
  51.     HttpClient client = new HttpClient();  
  52.       
  53.     String body ="[{\"userId\":1,\"password\":1}]";  
  54.     // Create a method instance.  
  55.     method(client,url,body);  
  56.       
  57.     url = "http://10.129.39.149:8090/ajax/getInitValueArr.action";  
  58.       
  59.     body = "[{\"count\":10,\"ignoreCase\":\"false\",\"paras\":[\"a%\"],\"queryId\":\"getMenu\"}]";  
  60.     method(client,url,body);  
  61.   }  
  62. }

案例2:

  1.    HttpClient c = new HttpClient();    
  2.     HttpMethod m = new GetMethod(); 
  3.  
  4. String path =msnListPath+"?account="+userSimpleInfo.getMsnAccount()+"&type=msn&uid="+userSimpleInfo.getPassportId()+"&pwd="+userSimpleInfo.getMsnPassword();
  5.     m.setPath(path); 
  6.     String response="";
  7.     int status = 0; 
  8.     try { 
  9.     status  = c.executeMethod(m);
  10.  
  11. if(status == HttpStatus.SC_OK) {
  12. byte[] rbytes = m.getResponseBody(); 
  13. response = new String(rbytes,"UTF-8"); 
  14. return  response;
  15. } 
  16.  
  17. } catch (HttpException e) { 
  18. // TODO Auto-generated catch block 
  19. e.printStackTrace(); 
  20. } catch (IOException e) { 
  21. // TODO Auto-generated catch block 
  22. e.printStackTrace(); 
  23. } 
  24. return "error";