国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

如何用java模擬ajax數(shù)據(jù)發(fā)送請(qǐng)求

2019-11-14 14:51:16
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

案例1:

import org.apache.commons.httpclient.*;  import org.apache.commons.httpclient.methods.*;  import org.apache.commons.httpclient.params.HttpMethodParams;    import java.io.*;    public class HttpClientTutorial {        PRivate static String url = "http://10.129.39.149:8090/Ajax/loginMgt/login.action";      public static void method(HttpClient client,String url,String body){        PostMethod  method = new PostMethod(url);          //"count":10,"ignoreCase":"false","paras":["a%"],"queryId":"getMenu"          NameValuePair[] postData = new NameValuePair[]{};            //postData[0] = new NameValuePair("count", 10);           method.setRequestBody(body);//addParameters(postData);                               // Provide custom retry handler is necessary          /*method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,                  new DefaultHttpMethodRetryHandler(3, false));*/            try {            // Execute the method.            int statusCode = client.executeMethod(method);              if (statusCode != HttpStatus.SC_OK) {              System.err.println("Method failed: " + method.getStatusLine());            }              // Read the response body.            byte[] responseBody = method.getResponseBody();              // Deal with the response.            // Use caution: ensure correct character encoding and is not binary data            System.out.println(new String(responseBody,"utf-8"));          } catch (HttpException e) {              System.err.println("Fatal protocol violation: " + e.getMessage());              e.printStackTrace();            } catch (IOException e) {              System.err.println("Fatal transport error: " + e.getMessage());              e.printStackTrace();            } finally {              // Release the connection.              method.releaseConnection();            }      }        public static void main(String[] args) {      // Create an instance of HttpClient.      HttpClient client = new HttpClient();            String body ="[{/"userId/":1,/"passWord/":1}]";      // Create a method instance.      method(client,url,body);            url = "http://10.129.39.149:8090/ajax/getInitValueArr.action";            body = "[{/"count/":10,/"ignoreCase/":/"false/",/"paras/":[/"a%/"],/"queryId/":/"getMenu/"}]";      method(client,url,body);    }  }

案例2:

   HttpClient c = new HttpClient();        HttpMethod m = new GetMethod(); String path =msnListPath+"?account="+userSimpleInfo.getMsnAccount()+"&type=msn&uid="+userSimpleInfo.getPassportId()+"&pwd="+userSimpleInfo.getMsnPassword();    m.setPath(path);     String response="";	    int status = 0;     try {     status  = c.executeMethod(m);	if(status == HttpStatus.SC_OK) {	byte[] rbytes = m.getResponseBody(); response = new String(rbytes,"UTF-8"); return  response;	} } catch (HttpException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return "error";



發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 伊吾县| 延津县| 新乡市| 门头沟区| 浦县| 镇远县| 吉木萨尔县| 元谋县| 德钦县| 望奎县| 辽宁省| 获嘉县| 惠来县| 南昌县| 息烽县| 两当县| 庄浪县| 新巴尔虎左旗| 玉溪市| 桐城市| 西吉县| 南宁市| 九龙坡区| 高邑县| 溆浦县| 敖汉旗| 莲花县| 罗江县| 乌苏市| 福清市| 察隅县| 鸡泽县| 宝山区| 上栗县| 双江| 江津市| 柳江县| 德江县| 甘洛县| 正宁县| 电白县|