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

          看到這個跟我寫的那個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,并返回它的內(nèi)容
           * <p>
           
          */

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

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

              
          public SendPost() {
                  
              }


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

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

              }


              
          /**//**
               * <p>
               * 本方法用于POST一個消息
               * <p>
               * 
               * 
          @param post
               *            要POST的參數(shù),比如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() {
                  
          /** 某一行的內(nèi)容 */
                  String line 
          = null;
                  
          /** 最終內(nèi)容 */
                  String result 
          = "";
                  
          try {
                      
          /** 打開到此 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);
              }

          }

          主站蜘蛛池模板: 师宗县| 临潭县| 曲靖市| 东山县| 无极县| 延长县| 建阳市| 镇康县| 苍南县| 徐闻县| 苗栗市| 临清市| 富源县| 杂多县| 汶川县| 普格县| 丁青县| 贺兰县| 延川县| 普宁市| 德江县| 郁南县| 德保县| 招远市| 新郑市| 广西| 剑川县| 霍邱县| 都兰县| 渑池县| 饶河县| 山阴县| 双桥区| 克什克腾旗| 邵东县| 宜君县| 禄劝| 宁河县| 井冈山市| 汉中市| 嘉峪关市|