posts - 19, comments - 53, trackbacks - 0, articles - 283
            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          日歷

          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          我參與的團(tuán)隊(duì)

          最新隨筆

          搜索

          •  

          積分與排名

          • 積分 - 358304
          • 排名 - 155

          URLConnection 獲取網(wǎng)絡(luò)資源--(摘)

          Posted on 2009-06-28 13:35 Gavin.lee 閱讀(486) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): java SE & EE

          看到這個(gè)跟我寫(xiě)的那個(gè)HttpUTLConnection工具類(lèi)差不多,就摘下來(lái)了,做了點(diǎn)修改,呵,作者看到了請(qǐng)別見(jiàn)怪,O(∩_∩)O~

          package com.Gavin.tools.util;

          import java.io.BufferedReader;
          import java.io.IOException;
          import java.io.InputStreamReader;
          import java.io.PrintWriter;
          import java.net.HttpURLConnection;
          import java.net.MalformedURLException;
          import java.net.URL;
          import java.net.URLConnection;

          /**//**
           * <p>
           * 本類(lèi)用于Post一個(gè)URL,并返回它的內(nèi)容
           * <p>
           
          */

          public class SendPost {
              
          /** url */
              
          private URL url;

              
          /** url連接 */
          //    private HttpURLConnection conn;    //HttpURLConnection 是支持 HTTP 特定功能的 URLConnection
              private URLConnection conn;

              
          public SendPost() {
                  
              }


              
          /**//**
               * <p>
               * 本方法根據(jù)一個(gè)字符串創(chuàng)建一個(gè)URL,并打開(kāi)URL的連接
               * <p>
               * 
               * 
          @param urlAddr
               *            URL地址
               
          */

              
          public void setURL(String urlAddr) {
                  
          try {
                      
          /** 創(chuàng)建一個(gè)URL */
                      url 
          = new URL(urlAddr);
                      
          /** 打開(kāi)URL連接 */
          //            conn = (HttpURLConnection)url.openConnection();
                      conn = url.openConnection();
                  }
           catch (MalformedURLException ex) {
                      
          /** 錯(cuò)誤URL產(chǎn)生異常 */
                      ex.printStackTrace();
                  }
           catch (IOException ex) {
                      
          /** 輸入輸出異常 */
                      ex.printStackTrace();
                  }

              }


              
          /**//**
               * <p>
               * 本方法用于POST一個(gè)消息
               * <p>
               * 
               * 
          @param post
               *            要POST的參數(shù),比如user=huiwanpeng&password=hwp##
               
          */

              
          public void sendPost(String post) {
                  
          /** 打算將URL連接進(jìn)行輸入 */
                  conn.setDoInput(
          true);
                  
          /** 打算將URL連接進(jìn)行輸出 */
                  conn.setDoOutput(
          true);
                  
          /** 聲明的一個(gè)打印輸出流 */
                  PrintWriter pw 
          = null;
                  
          try {
                      pw 
          = new PrintWriter(conn.getOutputStream());
                      pw.print(post);
                  }
           catch (IOException e) {
                      e.printStackTrace();
                  }
           finally {
                      pw.close();
                  }

              }


              
          public String getContent() {
                  
          /** 某一行的內(nèi)容 */
                  String line 
          = null;
                  
          /** 最終內(nèi)容 */
                  String result 
          = "";
                  
          try {
                      
          /** 打開(kāi)到此 URL 引用的資源的通信鏈接 */
                      conn.connect();
                      BufferedReader br 
          = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                      
          /** 一行一行地讀,直到讀完 */
                      
          while ((line = br.readLine()) != null{
                          result 
          += line + "\n";
                      }

                      
          /** 關(guān)閉連接 */
                      br.close();
                  }
           catch (IOException ex) {
                      ex.printStackTrace();
                  }

                  
          return result;
              }


              
          public static void main(String[] args) {
                  SendPost test 
          = new SendPost();
                  test.setURL(
          "http://www.ip138.com:8080/search.asp");
                  test.sendPost(
          "action=mobile&mobile=13501678250");
                  String aa 
          = test.getContent().trim();
                  System.out.println(aa);
              }

          }

          主站蜘蛛池模板: 灌阳县| 琼结县| 海门市| 潼南县| 灌南县| 蕲春县| 景东| 五大连池市| 金湖县| 仙游县| 怀宁县| 利辛县| 达拉特旗| 文水县| 英吉沙县| 金秀| 冷水江市| 富裕县| 乌苏市| 肥城市| 恭城| 延津县| 门源| 巩义市| 迁西县| 兴山县| 从江县| 海淀区| 崇明县| 柘城县| 贵定县| 盐山县| 嫩江县| 枝江市| 荣昌县| 乐山市| 浪卡子县| 南投县| 承德县| 房山区| 广平县|