2011-04-10 94 views
0

我有以下的代碼來獲得一個URL的內容..如何通過變量與HttpURLConnection的

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 

package javaapplication3; 

/** 
* 
* @author Ravi 
*/ 
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.ProtocolException; 
import java.net.URL; 

public class Main { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     HttpURLConnection connection = null; 
     OutputStreamWriter wr = null; 
     BufferedReader rd = null; 
     StringBuilder sb = null; 
     String line = null; 

     URL serverAddress = null; 

     try { 
      serverAddress = new URL("http://192.16.110.11:8084/RaviTest/index.jsp?id=3"); 
      //set up out communications stuff 
      connection = null; 

      //Set up the initial connection 
      connection = (HttpURLConnection)serverAddress.openConnection(); 
      connection.setRequestMethod("GET"); 
      connection.setDoOutput(true); 
      connection.setReadTimeout(10000); 

      connection.connect(); 

      //get the output stream writer and write the output to the server 
      //not needed in this example 
      //wr = new OutputStreamWriter(connection.getOutputStream()); 
      //wr.write(""); 
      //wr.flush(); 

      //read the result from the server 
      rd = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
      sb = new StringBuilder(); 

      while ((line = rd.readLine()) != null) 
      { 
       sb.append(line + '\n'); 
      } 

      System.out.println(sb.toString()); 

     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (ProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     finally 
     { 
      //close the connection, set all objects to null 
      connection.disconnect(); 
      rd = null; 
      sb = null; 
      wr = null; 
      connection = null; 
     } 
    } 
} 

但這個代碼只複製指定的URL的內容。

相反,如果我想連接到一個jsp頁面,我可以得到正在通過動態該JSP頁面發送的值...

例如,如果我有一個頁面http://192.16.110.51/WelcomeProject/index.jsp

我應該能夠傳遞一個變量,反過來index.jsp頁面會傳遞給我變量附加了hello world的變量,或者對變量做一些操作並將結果發送給我。如何實現這個功能?...如果不是HttpURLConnection ..有什麼其他的方式可以讓我做到這一點。

+0

你的意思是像Web服務? – 2011-04-10 19:27:25

回答

0

當您使用的是http客戶端時,您自動執行瀏覽器。瀏覽器不瞭解任何有關jsp頁面的信息。它不知道Java。它所知道的只是HTML和HTTP。

如果JSP頁面響應表單帖子,則可以發送帶有表單參數的HTTP POST。

Apache Commons Httpclient比使用原始JRE更容易,所以我建議您閱讀它。

0

這是從修改一個例子:http://www.java2s.com/Code/JavaAPI/java.net/URLConnectionsetDoOutputbooleandooutput.htm(我刪除響應部分,你可以真正想要返回)

import java.io.DataInputStream; 
import java.io.DataOutputStream; 
import java.net.URL; 
import java.net.URLConnection; 

public class MainClass { 
    public static void main(String args[]) throws Exception { 
    String query = "id=3"; 

    URLConnection uc = new URL("http://192.16.110.11:8084/RaviTest/index.jsp").openConnection(); 
    uc.setDoOutput(true); 
    uc.setAllowUserInteraction(false); 
    DataOutputStream dos = new DataOutputStream(uc.getOutputStream()); 

    // The POST line, the Accept line, and 
    // the content-type headers are sent by the URLConnection. 
    // We just need to send the data 
    dos.writeBytes(query); 
    dos.close(); 
    } 

} 
+0

嗨..你有wrtiitn dos.writeBytes(查詢),我想這將查詢傳遞給URl.But如何知道URL的查詢? – user650521 2011-04-10 19:51:04

+0

@ user650521它被髮送到'URLConnection OutputStream'。看看如何定義'DataOutputStream dos'。 http://download.oracle.com/javase/6/docs/api/java/net/URLConnection.html#getOutputStream%28%29 – Aleadam 2011-04-10 20:00:27