import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
public static String getMethod(String url,String apikey) {
GetMethod getMethod = new GetMethod(url);
if(apikey != null ){
getMethod.setRequestHeader("apikey",apikey);
}
HttpClient client = new HttpClient();
try {
client.getHttpConnectionManager().getParams().setConnectionTimeout(50000);
int status = client.executeMethod(getMethod);
if (status == HttpStatus.SC_OK) {
return getMethod.getResponseBodyAsString();
}
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
getMethod.releaseConnection();
}
return null;
}
需要引用http访问的jar包,然后调用即可