Java中的HTTP编程?
我想基于Java中的URL创建一个http请求,并更改它的一些头或添加新的头;然后,接收该请求的相应http响应,并获取其头值和内容。如何做到尽可能简单?您可以使用Apache HTTP客户端或使用标准的Java中的HTTP编程?,java,http,network-programming,Java,Http,Network Programming,我想基于Java中的URL创建一个http请求,并更改它的一些头或添加新的头;然后,接收该请求的相应http响应,并获取其头值和内容。如何做到尽可能简单?您可以使用Apache HTTP客户端或使用标准的HttpUrlConnection URL url = new URL("http://thehost/thepath/"); HttpURLConnection connection = (HttpURLConnection) url.openConnection();
HttpUrlConnection
URL url = new URL("http://thehost/thepath/");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod(method); // GET, POST, ...
connection.setDoOutput(true);
connection.setDoInput(true);
connection.addRequestProperty(key, value); // this way you can set HTTP header
您可以使用Apache HTTP客户端,也可以使用标准的
HttpUrlConnection
URL url = new URL("http://thehost/thepath/");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod(method); // GET, POST, ...
connection.setDoOutput(true);
connection.setDoInput(true);
connection.addRequestProperty(key, value); // this way you can set HTTP header
实际上,使用ApacheHttpClient 4.x并使用ResponseHandler HttpClient有很多您可能希望原始JavaAPI不提供的好东西,比如多线程使用、连接池、对各种身份验证机制的支持等等 下面是一个简单的示例,它执行get并将主体作为字符串返回给您
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
...
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://www.google.com");
httpGet.addHeader("MyHeader", "MyValue");
try {
String body = httpClient.execute(httpGet, new ResponseHandler<String>() {
@Override
public String handleResponse(HttpResponse response) throws IOException {
Header firstHeader = response.getFirstHeader("MyHeader");
String headerValue = firstHeader.getValue();
return EntityUtils.toString(response.getEntity());
}
});
} catch (IOException e) {
e.printStackTrace();
}
import org.apache.http.HttpResponse;
导入org.apache.http.client.ClientProtocolException;
导入org.apache.http.client.ResponseHandler;
导入org.apache.http.client.methods.HttpGet;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.apache.http.util.EntityUtils;
...
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpGet HttpGet=新的HttpGet(“http://www.google.com");
addHeader(“MyHeader”、“MyValue”);
试一试{
String body=httpClient.execute(httpGet,newresponseHandler(){
@凌驾
公共字符串HandlerResponse(HttpResponse响应)引发IOException{
Header firstHeader=response.getFirstHeader(“MyHeader”);
字符串headerValue=firstHeader.getValue();
返回EntityUtils.toString(response.getEntity());
}
});
}捕获(IOE异常){
e、 printStackTrace();
}
确实要使用ApacheHttpClient 4.x和ResponseHandler
HttpClient有很多您可能希望原始JavaAPI不提供的好东西,比如多线程使用、连接池、对各种身份验证机制的支持等等
下面是一个简单的示例,它执行get并将主体作为字符串返回给您
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
...
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://www.google.com");
httpGet.addHeader("MyHeader", "MyValue");
try {
String body = httpClient.execute(httpGet, new ResponseHandler<String>() {
@Override
public String handleResponse(HttpResponse response) throws IOException {
Header firstHeader = response.getFirstHeader("MyHeader");
String headerValue = firstHeader.getValue();
return EntityUtils.toString(response.getEntity());
}
});
} catch (IOException e) {
e.printStackTrace();
}
import org.apache.http.HttpResponse;
导入org.apache.http.client.ClientProtocolException;
导入org.apache.http.client.ResponseHandler;
导入org.apache.http.client.methods.HttpGet;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.apache.http.util.EntityUtils;
...
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpGet HttpGet=新的HttpGet(“http://www.google.com");
addHeader(“MyHeader”、“MyValue”);
试一试{
String body=httpClient.execute(httpGet,newresponseHandler(){
@凌驾
公共字符串HandlerResponse(HttpResponse响应)引发IOException{
Header firstHeader=response.getFirstHeader(“MyHeader”);
字符串headerValue=firstHeader.getValue();
返回EntityUtils.toString(response.getEntity());
}
});
}捕获(IOE异常){
e、 printStackTrace();
}
使用Apache HTTP客户端。Java还有URL类,您可以使用它创建URLConnection对象。使用Java的URL类创建URLConnection、设置头和发送请求的示例代码:@nhahtdh正确。你也可以使用谷歌搜索服务,使用ApacheHTTP客户端。Java还有URL类,您可以使用它创建URLConnection对象。使用Java的URL类创建URLConnection、设置头和发送请求的示例代码:@nhahtdh正确。你也可以使用谷歌搜索服务。