Java调用http post接口
时间: 2023-09-15 09:15:14 浏览: 34
可以使用Java自带的HttpURLConnection或者第三方库如HttpClient来实现。以下是一个使用HttpURLConnection的示例代码:
```
URL url = new URL("http://example.com/api");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/json");
String requestBody = "{\"key\": \"value\"}";
OutputStream outputStream = conn.getOutputStream();
outputStream.write(requestBody.getBytes());
outputStream.flush();
outputStream.close();
int responseCode = conn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream inputStream = conn.getInputStream();
// 处理响应数据
} else {
// 处理错误情况
}
```
相关问题
java 调用http post接口
可以使用 Java 的 HttpURLConnection 类来实现 HTTP POST 请求。以下是一个简单的示例:
```
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.IOException;
public class HttpPostExample {
public static void main(String[] args) throws IOException {
String url = "http://example.com/api";
String postData = "param1=value1¶m2=value2";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// 设置请求方法为 POST
con.setRequestMethod("POST");
// 设置请求头部信息
con.setRequestProperty("User-Agent", "Java client");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
// 启用输出流
con.setDoOutput(true);
// 将 POST 数据写入请求体
OutputStream os = con.getOutputStream();
os.write(postData.getBytes());
os.flush();
os.close();
// 获取响应状态码
int responseCode = con.getResponseCode();
// 读取响应内容
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// 打印响应结果
System.out.println("Response code: " + responseCode);
System.out.println("Response body: " + response.toString());
}
}
```
在上面的示例中,我们首先创建一个 URL 对象,用于表示要访问的 API 地址。然后,我们使用 HttpURLConnection 类来打开连接,并设置请求方法为 POST。接下来,我们设置请求头部信息,启用输出流,并将 POST 数据写入请求体。最后,我们读取响应内容,并将其打印出来。
注意,这里的 POST 数据是以字符串形式传递的,可以根据实际需要进行修改。另外,如果 API 返回的是 JSON 格式的数据,可以使用 JSON 库来解析响应结果。
java 调用http接口post
在Java中调用HTTP接口的POST请求,可以使用Java标准库中的`java.net.HttpURLConnection`类。
示例代码如下:
```
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpPostExample {
public static void main(String[] args) {
try {
// 指定接口地址
URL url = new URL("http://your-interface-address");
// 打开连接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 设置请求方式
conn.setRequestMethod("POST");
// 设置请求头
conn.setRequestProperty("Content-Type", "application/json");
conn.setDoOutput(true);
// 构造请求参数
String data = "{\"param1\":\"value1\",\"param2\":\"value2\"}";
// 发送请求
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.writeBytes(data);
wr.flush();
wr.close();
// 获取响应
int responseCode = conn.getResponseCode();
System.out.println("Response Code: " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// 打印响应
System.out.println(response.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
需要注意的是, 需要引入相应的包,上述代码需要用到URL, HttpURLConnection, DataOutputStream,BufferedReader等类
其中,参数 `data` 是请求的参数,格式为字符串。
通过上述代码可以发送一个 POST 请求,可以在响应的基础上进一步操作