我有以下的代碼來獲得一個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 ..有什麼其他的方式可以讓我做到這一點。
你的意思是像Web服務? – 2011-04-10 19:27:25