這裏是一個了HTTPClient我做:
public class HTTPClient {
/**
* Request the HTTP page.
* @param uri the URI to request.
* @return the HTTP page in a String.
*/
public String request(String uri){
// Create a new HTTPClient.
HttpClient client = new HttpClient();
// Set the parameters
client.getParams().setParameter("http.useragent", "Test Client");
//5000ms
client.getParams().setParameter("http.connection.timeout",new Integer(5000));
GetMethod method = new GetMethod();
try{
method.setURI(new URI(uri, true));
int returnCode = client.executeMethod(method);
// The Get method failed.
if(returnCode != HttpStatus.SC_OK){
System.err.println("Unable to fetch default page," +
" status code : " + returnCode);
}
InputStream in = method.getResponseBodyAsStream();
// The input isn't null.
if (in != null) {
StringBuilder sb = new StringBuilder();
String line;
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
} finally {
in.close();
}
//return statement n0
return sb.toString();
}
else {
//return statement n1
return null;
}
} catch (HttpException he) {
System.err.println(" HTTP failure");
} catch (IOException ie) {
System.err.println(" I/O problem");
} finally {
method.releaseConnection();
}
//return statement n2
return null;
}
}
也許它可以幫助你。
您使用的是什麼HTTP客戶端API? – BalusC 2011-02-09 15:10:32