java构造http请求的几种方式(附源码)

我来为您详细讲解Java构造HTTP请求的几种方式。

我来为您详细讲解"Java构造HTTP请求的几种方式"。

1. 使用URLConnection发送HTTP请求

使用URLConnection可以方便的发送HTTP请求。下面是一个使用URLConnection发送get请求的示例代码:

public static String sendGetRequest(String url) throws Exception {
    URL getUrl = new URL(url);
    HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
    connection.connect();//建立链接
    BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String lines;
    StringBuilder sb = new StringBuilder();
    while ((lines = reader.readLine()) != null) {
        sb.append(lines);
    }
    reader.close();
    connection.disconnect();
    return sb.toString();
}

使用HttpURLConnection发送HttpPost请求的示例代码:

public static String sendPostRequest(String url, String data) throws Exception {
    URL postUrl = new URL(url);
    HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
    connection.setRequestMethod("POST");
    connection.setDoOutput(true);//如果后台需要传参,则必须设置为true,否则会抛出ProtocolException异常
    connection.getOutputStream().write(data.getBytes());
    BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String line;
    StringBuilder sb = new StringBuilder();
    while ((line = reader.readLine()) != null) {
        sb.append(line);
    }
    reader.close();
    connection.disconnect();
    return sb.toString();
}

2. 使用HttpClient发送HTTP请求

使用Apache的HttpClient包发送HTTP请求也是非常方便的,相关的jar包下载地址:http://hc.apache.org/downloads.cgi。下面是一个使用HttpClient发送get请求的示例代码:

public static String sendGetRequestWithHttpClient(String url) throws Exception {
    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet(url);
    HttpResponse response = client.execute(request);
    HttpEntity entity = response.getEntity();
    InputStream content = entity.getContent();
    BufferedReader reader = new BufferedReader(new InputStreamReader(content));
    String line;
    StringBuilder sb = new StringBuilder();
    while ((line = reader.readLine()) != null) {
        sb.append(line);
    }
    reader.close();
    content.close();
    return sb.toString();
}

使用HttpClient发送HttpPost请求的示例代码:

public static String sendPostRequestWithHttpClient(String url, String data) throws Exception {
    HttpClient client = new DefaultHttpClient();
    HttpPost request = new HttpPost(url);
    StringEntity stringEntity = new StringEntity(data);
    stringEntity.setContentType("application/json");
    request.setEntity(stringEntity);
    HttpResponse response = client.execute(request);
    HttpEntity entity = response.getEntity();
    InputStream content = entity.getContent();
    BufferedReader reader = new BufferedReader(new InputStreamReader(content));
    String line;
    StringBuilder sb = new StringBuilder();
    while ((line = reader.readLine()) != null) {
        sb.append(line);
    }
    reader.close();
    content.close();
    return sb.toString();
}

以上就是关于使用Java构造HTTP请求的几种方式,希望对您有所帮助。

本文标题为:java构造http请求的几种方式(附源码)

基础教程推荐