今天,終于明白了別人是怎么在網上抓數據的.
package com.roadway.phserver.test;

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

/**
* <p>
* 本類用于Post一個URL,并返回它的內容
* <p>
*
* @author huiwanpeng
* @time 2008-1-24
* @see no
*/
public class SendPost {
/** url */
private URL url;

/** url連接 */
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 = 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) {
String urlAddr = "http://www.ip138.com:8080/search.asp";
String post = "action=mobile&mobile=13501678250";
SendPost test = new SendPost();
test.setURL(urlAddr);
test.sendPost(post);
String aa = test.getContent().trim();
System.out.println(aa);
}
}












































































































