java HttpURLConnection來實作get及post動作

這個範例可以利用java.net.HttpURLconnection或HttpsURLconnection來摸擬瀏覽網頁

做form submit動作

public boolean doPost(String sURL,String data,String cookie,String referer,String charset)

post部份需要傳入

  • sURL:Action的url
  • data :要傳送的的資料也就是像id=123&test=456之類的
  • cookie:是否要傳送cookie資料,可為null,像 __utma=114386561.1334910113.1250671126.1251247266.1251279995.24;
  • referer:傳那裡來的,是一個網址,可為null
  • charset:傳送及取回的資料編碼為何
public boolean doGet(String sURL,String cookie,String referer,String charset)

跟post唯一不同的地方為

  • sURL:Action的url 再加上?data ,像http://www.aaa.com/123.jsp?id=123&test=456

ps.範例使用log4j,如果不會用的人可以直接把log4j部份改成System.out.println

private org.apache.log4j.Logger logger; 
public WebModule() { 
logger = org.apache.log4j.Logger.getLogger(this.getClass()); 
} 
public boolean doPost(String sURL, String data, String cookie, 
String referer, String charset) { 
boolean doSuccess = false; 
java.io.BufferedWriter wr = null; 
try { 
URL url = new URL(sURL); 
HttpURLConnection URLConn = (HttpURLConnection) url 
.openConnection(); 
URLConn.setDoOutput(true); 
URLConn.setDoInput(true); 
((HttpURLConnection) URLConn).setRequestMethod("POST"); 
URLConn.setUseCaches(false); 
URLConn.setAllowUserInteraction(true); 
HttpURLConnection.setFollowRedirects(true); 
URLConn.setInstanceFollowRedirects(true); 
URLConn 
.setRequestProperty( 
"User-agent", 
"Mozilla/5.0 (Windows; U; Windows NT 6.0; zh-TW; rv:1.9.1.2) " 
+ "Gecko/20090729 Firefox/3.5.2 GTB5 (.NET CLR 3.5.30729)"); 
URLConn 
.setRequestProperty("Accept", 
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); 
URLConn.setRequestProperty("Accept-Language", 
"zh-tw,en-us;q=0.7,en;q=0.3"); 
URLConn.setRequestProperty("Accept-Charse", 
"Big5,utf-8;q=0.7,*;q=0.7"); 
if (cookie != null) 
URLConn.setRequestProperty("Cookie", cookie); 
if (referer != null) 
URLConn.setRequestProperty("Referer", referer); 
URLConn.setRequestProperty("Content-Type", 
"application/x-www-form-urlencoded"); 
URLConn.setRequestProperty("Content-Length", String.valueOf(data 
.getBytes().length)); 
java.io.DataOutputStream dos = new java.io.DataOutputStream(URLConn 
.getOutputStream()); 
dos.writeBytes(data); 
java.io.BufferedReader rd = new java.io.BufferedReader( 
new java.io.InputStreamReader(URLConn.getInputStream(), 
charset)); 
String line; 
while ((line = rd.readLine()) != null) { 
System.out.println(line); 
} 
rd.close(); 
} catch (java.io.IOException e) { 
doSuccess = false; 
logger.info(e); 
} finally { 
if (wr != null) { 
try { 
wr.close(); 
} catch (java.io.IOException ex) { 
logger.info(ex); 
} 
wr = null; 
} 
} 
return doSuccess; 
} 
public boolean doGet(String sURL, String cookie, String referer, 
String charset) { 
boolean doSuccess = false; 
BufferedReader in = null; 
try { 
URL url = new URL(sURL); 
HttpURLConnection URLConn = (HttpURLConnection) url 
.openConnection(); 
URLConn 
.setRequestProperty( 
"User-agent", 
"Mozilla/5.0 (Windows; U; Windows NT 6.0; zh-TW; rv:1.9.1.2) " 
+ "Gecko/20090729 Firefox/3.5.2 GTB5 (.NET CLR 3.5.30729)"); 
URLConn 
.setRequestProperty("Accept", 
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); 
URLConn.setRequestProperty("Accept-Language", 
"zh-tw,en-us;q=0.7,en;q=0.3"); 
URLConn.setRequestProperty("Accept-Charse", 
"Big5,utf-8;q=0.7,*;q=0.7"); 
if (cookie != null) 
URLConn.setRequestProperty("Cookie", cookie); 
if (referer != null) 
URLConn.setRequestProperty("Referer", referer); 
URLConn.setDoInput(true); 
URLConn.setDoOutput(true); 
URLConn.connect(); 
URLConn.getOutputStream().flush(); 
in = new BufferedReader(new InputStreamReader(URLConn 
.getInputStream(), charset)); 
String line; 
while ((line = in.readLine()) != null) { 
System.out.println(line); 
} 
} catch (IOException e) { 
doSuccess = false; 
log.out.println(e); 
e.printStackTrace(); 
} finally { 
if (in != null) { 
try { 
in.close(); 
} catch (java.io.IOException ex) { 
logger.info(ex); 
} 
in = null; 
} 
} 
return doSuccess; 
}

如果要使用SSL來取得資料可以用下面的source

public boolean doPostSSL(String sURL, String data, String referer, String charset) {
buff = null;
buff = new StringBuffer();
boolean doSuccess = true;
java.io.BufferedWriter wr = null;
try {
URL url = new URL(sURL);
// Proxy proxy = new Proxy(Proxy.Type.HTTP, new
// InetSocketAddress("proxy.hinet.net", 80));
// trustAllHosts();
HttpsURLConnection URLConn = (HttpsURLConnection) url.openConnection();
URLConn.setDoOutput(true);
URLConn.setDoInput(true);
URLConn.setRequestMethod("POST");
URLConn.setUseCaches(false);
// URLConn.setHostnameVerifier(DO_NOT_VERYFY);
URLConn.setAllowUserInteraction(true);
HttpsURLConnection.setFollowRedirects(true);
// URLConn.setInstanceFollowRedirects(true);
URLConn.setInstanceFollowRedirects(false);
URLConn.setRequestProperty("User-agent",
"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36");
URLConn.setRequestProperty("Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
URLConn.setRequestProperty("Accept-Encoding", "gzip,deflate,sdch");
URLConn.setRequestProperty("Accept-Language", "zh-TW,zh;q=0.8,en-US;q=0.6,en;q=0.4");
// URLConn.setRequestProperty("Accept-Charse",
// "Big5,utf-8;q=0.7,*;q=0.7");
if (sessionid != null)
URLConn.setRequestProperty("Cookie", sessionid);
if (referer != null)
URLConn.setRequestProperty("Referer", referer);
URLConn.setReadTimeout(30000);
URLConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
URLConn.setRequestProperty("Content-Length", String.valueOf(data.getBytes().length));
java.io.DataOutputStream dos = new java.io.DataOutputStream(URLConn.getOutputStream());
dos.writeBytes(data);
InputStream ins = null;
// System.out.println("111111111111111\r\n"+URLConn.getContentEncoding()+"\r\n------------------------");
if (URLConn.getContentEncoding() != null && !URLConn.getContentEncoding().equals("")) {
String encode = URLConn.getContentEncoding().toLowerCase();
if (encode != null && !encode.equals("") && encode.indexOf("gzip") >= 0) {
ins = new GZIPInputStream(URLConn.getInputStream());
}
}
if (ins == null)
ins = URLConn.getInputStream();
java.io.BufferedReader rd = new java.io.BufferedReader(new java.io.InputStreamReader(ins, charset));
String line;
while ((line = rd.readLine()) != null) {
buff.append(line + "\r\n");
}
if (sessionid == null) {
String key;
if (URLConn != null) {
for (int i = 1; (key = URLConn.getHeaderFieldKey(i)) != null; i++) {
if (key.equalsIgnoreCase("set-cookie")) {
sessionid = URLConn.getHeaderField(key);
sessionid = sessionid.substring(0, sessionid.indexOf(";"));
}
}
}
}
// System.out.println("------------------------------------"+sessionid);
rd.close();
} catch (java.io.IOException e) {
doSuccess = false;
logger.info(e);
} finally {
if (wr != null) {
try {
wr.close();
} catch (java.io.IOException ex) {
logger.info(ex);
ex.printStackTrace();
}
wr = null;
}
}
return doSuccess;
}
static {
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[] {};
}
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
} };
// Install the all-trusting trust manager
try {
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
e.printStackTrace();
}
}

如果要取得error code 像是405 ,505之後的訊息或內容可以使用下面的source

在getInputStream時如果有error時會跑到IOException,此時取得Error Stream來做處理

	    		  InputStream error = URLConn.getErrorStream();
String line = "";
try {
int data = error.read();
while (data != -1) {
line = line + (char)data;
data = error.read();
}
error.close();
} catch (Exception ex) {
try {
if (error != null) {
error.close();
}
} catch (Exception exx) {
}
}

13 thoughts to “java HttpURLConnection來實作get及post動作”

  1. 這篇文章太棒啦,讓我不必用到「HttpClient」就能讓網頁執行post動作,真是獲益良多,謝謝作者的教學。
    還有其他文章的教學都很讚和很有幫助,有看有推,雖然懶的一篇一篇回,只有留這篇留言 意思意思,但總之每篇文章都大推,謝謝作者大方地傾囊相授啦~

    1. 謝謝你的讚美…
      如果有什麼特別需求也可以跟我講…
      我有時間會做的範例的^^ (能力之內)

  2. hihi yku
    看完你的文章 存在很多疑問
    要將android與PC做連接 1個圖檔用網路的方式上傳到PC
    這地方我一直想不透要怎麼做
    能否請你指教…
    By初學的Please

    1. 一個圖檔可以利用httpurlconnection的post stream上傳至Server
      就如你上一篇問的一樣,也可以提供一個網站讓android使用者自己選取檔案上傳

  3. hihi
    能否請教 將一個圖片透過網路上傳到電腦且讓圖片回傳到android
    這點要從哪裡動工 請指教
    多謝~

    1. 上傳的方式上一篇有回覆,那回傳到android部份 你就利用HttpUrlConnection把要接收的圖檔利用到getInputStream的串流把它存成檔案

  4. 請問大大

    我照著http://www.myandroid.tw/bbs-topic-21.sea
    的教學作

    他都無法跟網頁溝通

    請問是我哪裡沒弄到?

    我有設定權限

    1. 你可以試著用它的程式 去抓tw.yahoo.com的內容回來嘛?
      如果可以,那就是你server的設定配合有問題
      1.是不是port不對
      2.連接ip是否在同一個區段,或Server是否可對外
      3……
      如果不能取回內容,那程式碼就有狀況了!
      你給的資訊不對,我也只能這樣子猜而已

發表迴響