亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫(kù)
最近訪問板塊 發(fā)新帖
查看: 2129 | 回復(fù): 2
打印 上一主題 下一主題

[Android] java實(shí)現(xiàn)自動(dòng)訪問網(wǎng)站并獲取返回值的代碼 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2015-07-07 11:23 |只看該作者 |倒序?yàn)g覽
最近在做一個(gè)項(xiàng)目,里面有一個(gè)需求是需要通過java程序訪問一個(gè)http的接口,并且需要解析及獲取接口返回值,最后在網(wǎng)上查了資料,實(shí)現(xiàn)了此功能,將代碼和大家分享下。
通過java訪問http地址的實(shí)現(xiàn)方式有兩種,一種是post的方式,另外一種是get方式,下面就對(duì)兩種方式分別做一個(gè)介紹

[Java]代碼
  1. 1、java代碼實(shí)現(xiàn)自動(dòng)訪問網(wǎng)站的代碼
  2. try {
  3.     /** post方式 */
  4.     HttpClient client = new HttpClient();
  5.     PostMethod postMethod = new PostMethod(
  6.             "http://localhost:8080/portal/check.jsp");
  7.     // 參數(shù)設(shè)置
  8.     postMethod.setParameter("channelid", "85");
  9.     // 執(zhí)行postMethod
  10.     client.getParams().setParameter(
  11.             HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
  12.     // 執(zhí)行并返回狀態(tài)
  13.     int status = client.executeMethod(postMethod);
  14.     String getUrl = postMethod.getResponseBodyAsString();
  15.     System.out.println(getUrl);
  16. } catch (Exception e) {
  17.     // TODO Auto-generated catch block
  18.     e.printStackTrace();
  19. }
  20. 2、check.jsp類的代碼
  21. <%@ page contentType="text/html;charset=UTF-8" pageEncoding="utf-8" %>
  22. <%
  23. //通過欄目id判斷用戶對(duì)欄目權(quán)限;
  24. String channelid = (String) request.getParameter("channelid");
  25. if(null==channelid||""==channelid){
  26.     response.getWriter().print(100);
  27. }else{
  28.     response.getWriter().print(200);
  29. }
  30. %>
  31. 3、其輸出的結(jié)果為200.
  32. get方式實(shí)現(xiàn)
  33. // 構(gòu)造HttpClient的實(shí)例
  34. HttpClient httpClient = new HttpClient();
  35. // 創(chuàng)建GET方法的實(shí)例
  36. GetMethod getMethod = new GetMethod(url);
  37. // 使用系統(tǒng)提供的默認(rèn)的恢復(fù)策略
  38. getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
  39.         new DefaultHttpMethodRetryHandler());
  40. // 定義一個(gè)輸入流
  41. InputStream ins = null;
  42. // 定義文件流
  43. BufferedReader br = null;
  44. try {
  45.     // 執(zhí)行g(shù)etMethod
  46.     int statusCode = httpClient.executeMethod(getMethod);
  47.     if (statusCode != HttpStatus.SC_OK) {
  48.         System.err.println("方法失敗: " + getMethod.getStatusLine());
  49.     }
  50.     // 使用getResponseBodyAsStream讀取頁(yè)面內(nèi)容,
  51.     //這個(gè)方法對(duì)于目標(biāo)地址中有大量數(shù)據(jù)需要傳輸是最佳的。
  52.     ins = getMethod.getResponseBodyAsStream();
  53.     String charset = getMethod.getResponseCharSet();
  54.     if (charset.toUpperCase().equals("ISO-8859-1")) {
  55.         charset = "gbk";
  56.     }
  57.     // 按服務(wù)器編碼字符集構(gòu)建文件流,這里的CHARSET要根據(jù)實(shí)際情況設(shè)置
  58.     br = new BufferedReader(new InputStreamReader(ins, getMethod
  59.             .getResponseCharSet()));
  60.     StringBuffer sbf = new StringBuffer();
  61.     String line = null;
  62.     while ((line = br.readLine()) != null) {
  63.         sbf.append(line);
  64.     }
  65.     String result = new String(sbf.toString().getBytes(
  66.             getMethod.getResponseCharSet()), charset);
  67.     // 輸出內(nèi)容
  68.     System.out.println(result);
  69.   
  70.     // 服務(wù)器編碼
  71.     System.out.println("服務(wù)器編碼是:" + getMethod.getResponseCharSet());
  72.     return result;
  73. } catch (HttpException e) {
  74.     // 發(fā)生致命的異常,可能是協(xié)議不對(duì)或者返回的內(nèi)容有問題
  75.     System.out.println("請(qǐng)檢查您所提供的HTTP地址!");
  76.     e.printStackTrace();
  77. } catch (IOException e) {
  78.     // 發(fā)生網(wǎng)絡(luò)異常
  79.     e.printStackTrace();
  80. } finally {
  81.     // 關(guān)閉流,釋放連接
  82. }
  83. return null;
復(fù)制代碼

論壇徽章:
59
2015七夕節(jié)徽章
日期:2015-08-24 11:17:25ChinaUnix專家徽章
日期:2015-07-20 09:19:30每周論壇發(fā)貼之星
日期:2015-07-20 09:19:42ChinaUnix元老
日期:2015-07-20 11:04:38榮譽(yù)版主
日期:2015-07-20 11:05:19巳蛇
日期:2015-07-20 11:05:26CU十二周年紀(jì)念徽章
日期:2015-07-20 11:05:27IT運(yùn)維版塊每日發(fā)帖之星
日期:2015-07-20 11:05:34操作系統(tǒng)版塊每日發(fā)帖之星
日期:2015-07-20 11:05:36程序設(shè)計(jì)版塊每日發(fā)帖之星
日期:2015-07-20 11:05:40數(shù)據(jù)庫(kù)技術(shù)版塊每日發(fā)帖之星
日期:2015-07-20 11:05:432015年辭舊歲徽章
日期:2015-07-20 11:05:44
2 [報(bào)告]
發(fā)表于 2015-07-11 20:43 |只看該作者
使用Apache HTTPClient很容易的嘛。

論壇徽章:
80
20周年集字徽章-慶
日期:2020-10-28 14:09:1215-16賽季CBA聯(lián)賽之北京
日期:2020-10-28 13:32:5315-16賽季CBA聯(lián)賽之北控
日期:2020-10-28 13:32:4815-16賽季CBA聯(lián)賽之天津
日期:2020-10-28 13:13:35黑曼巴
日期:2020-10-28 12:29:1520周年集字徽章-周	
日期:2020-10-31 15:10:0720周年集字徽章-20	
日期:2020-10-31 15:10:07ChinaUnix元老
日期:2015-09-29 11:56:3020周年集字徽章-年
日期:2020-10-28 14:14:56
3 [報(bào)告]
發(fā)表于 2015-07-22 15:59 |只看該作者
uRLConnection.getResponseCode();這個(gè)方法可以直接獲取結(jié)果
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國(guó)互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP