無線&移動互聯網技術研發

          換位思考·····
          posts - 19, comments - 53, trackbacks - 0, articles - 283
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          日歷

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          我參與的團隊

          最新隨筆

          搜索

          •  

          積分與排名

          • 積分 - 357795
          • 排名 - 155

          URLConnection 獲取網絡資源--(摘)

          Posted on 2009-06-28 13:35 Gavin.lee 閱讀(485) 評論(0)  編輯  收藏 所屬分類: java SE & EE

          看到這個跟我寫的那個HttpUTLConnection工具類差不多,就摘下來了,做了點修改,呵,作者看到了請別見怪,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>
           * 本類用于Post一個URL,并返回它的內容
           * <p>
           
          */

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

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

              
          public SendPost() {
                  
              }


              
          /**//**
               * <p>
               * 本方法根據一個字符串創建一個URL,并打開URL的連接
               * <p>
               * 
               * 
          @param urlAddr
               *            URL地址
               
          */

              
          public void setURL(String urlAddr) {
                  
          try {
                      
          /** 創建一個URL */
                      url 
          = new URL(urlAddr);
                      
          /** 打開URL連接 */
          //            conn = (HttpURLConnection)url.openConnection();
                      conn = url.openConnection();
                  }
           catch (MalformedURLException ex) {
                      
          /** 錯誤URL產生異常 */
                      ex.printStackTrace();
                  }
           catch (IOException ex) {
                      
          /** 輸入輸出異常 */
                      ex.printStackTrace();
                  }

              }


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

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

              }


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

          }

          主站蜘蛛池模板: 栾城县| 扶余县| 岗巴县| 乡宁县| 民权县| 白山市| 临桂县| 民和| 石门县| 涿州市| 平度市| 饶平县| 乐至县| 阜新| 大石桥市| 平舆县| 泰顺县| 大丰市| 宣化县| 腾冲县| 东乡县| 牡丹江市| 额敏县| 湖北省| 安泽县| 金沙县| 三明市| 黎城县| 百色市| 屏东市| 新蔡县| 岐山县| 顺平县| 泰兴市| 阳信县| 青海省| 武清区| 宁武县| 闻喜县| 峨山| 崇文区|