如何在Java中发送HTTP请求? [英] How to send HTTP request in java?

查看:1304
本文介绍了如何在Java中发送HTTP请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,如何编写HTTP请求消息并将其发送到HTTP WebServer? 您可以使用< a href =http://download.oracle.com/javase/7/docs/api/java/net/HttpURLConnection.html =noreferrer> java.net.HttpUrlConnection 。



示例( from here ),并加以改进。包含在链接的情况下:

  public static String executePost(String targetURL,String urlParameters){
HttpURLConnection connection =空值;

尝试{
//创建连接
URL url = new URL(targetURL);
connection =(HttpURLConnection)url.openConnection();
connection.setRequestMethod(POST);
connection.setRequestProperty(Content-Type,
application / x-www-form-urlencoded);

connection.setRequestProperty(Content-Length,
Integer.toString(urlParameters.getBytes()。length));
connection.setRequestProperty(Content-Language,en-US);

connection.setUseCaches(false);
connection.setDoOutput(true);
$ b $ //发送请求
DataOutputStream wr = new DataOutputStream(
connection.getOutputStream());
wr.writeBytes(urlParameters);
wr.close();

//获取响应
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
StringBuilder response = new StringBuilder(); //或StringBuffer,如果Java版本5+
String line; ((line = rd.readLine())!= null){
response.append(line);
while
response.append('\r');
}
rd.close();
return response.toString();
} catch(Exception e){
e.printStackTrace();
返回null;
} finally {
if(connection!= null){
connection.disconnect();
}
}
}


In Java, How to compose a HTTP request message and send it to a HTTP WebServer?

解决方案

You can use java.net.HttpUrlConnection.

Example (from here), with improvements. Included in case of link rot:

public static String executePost(String targetURL, String urlParameters) {
  HttpURLConnection connection = null;

  try {
    //Create connection
    URL url = new URL(targetURL);
    connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", 
        "application/x-www-form-urlencoded");

    connection.setRequestProperty("Content-Length", 
        Integer.toString(urlParameters.getBytes().length));
    connection.setRequestProperty("Content-Language", "en-US");  

    connection.setUseCaches(false);
    connection.setDoOutput(true);

    //Send request
    DataOutputStream wr = new DataOutputStream (
        connection.getOutputStream());
    wr.writeBytes(urlParameters);
    wr.close();

    //Get Response  
    InputStream is = connection.getInputStream();
    BufferedReader rd = new BufferedReader(new InputStreamReader(is));
    StringBuilder response = new StringBuilder(); // or StringBuffer if Java version 5+
    String line;
    while ((line = rd.readLine()) != null) {
      response.append(line);
      response.append('\r');
    }
    rd.close();
    return response.toString();
  } catch (Exception e) {
    e.printStackTrace();
    return null;
  } finally {
    if (connection != null) {
      connection.disconnect();
    }
  }
}

这篇关于如何在Java中发送HTTP请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆