java 向url发送post请求



发送请求的大致过程如下:


String url=" http://www.baidu.com ";

HttpPost httppost=new HttpPost(url);    //建立HttpPost对象

List<NameValuePair> params=new ArrayList<NameValuePair>();   //建立一个NameValuePair数组,用于存储欲传送的参数

params.add(new BasicNameValuePair("pwd","2544"));//添加参数


httppost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));    //设置编码


HttpResponse response=new DefaultHttpClient().execute(httppost);   //发送Post,并返回一个HttpResponse对象

完整的向url发送post请求带参数代码

public static String searchUrl = "http://192.168.0.90:9999/ml/predict_credit";

 final CloseableHttpClient httpClient = HttpClients.createDefault();
final HttpPost httpPost = new HttpPost(searchUrl);
final List<BasicNameValuePair> pairList = new ArrayList<>();
pairList.add(new BasicNameValuePair("客户编码", map.get("consumerCode")));
pairList.add(new BasicNameValuePair("借款人行业", map.get("jobIndustry")));
CloseableHttpResponse response = null;
try {
httpPost.setEntity(new UrlEncodedFormEntity(pairList, "utf-8"));
response = httpClient.execute(httpPost);
final HttpEntity httpEntity = response.getEntity();
final ObjectMapper mapper = new ObjectMapper();
@SuppressWarnings("unchecked")
final Map<String, Object> values = mapper.readValue(httpEntity.getContent(), Map.class);
LOGGER.info("[ML result]:" + values);
return values;
} catch (final Exception e) {
LOGGER.debug("execute httpPost error");
return null;
} finally {
try {
response.close();
httpClient.close();
} catch (final Exception e) {
LOGGER.debug("Http client close error.");
throw new RuntimeException(e);
}
}


}




如果您想在Java中模仿XWW发送POST请求,您可以使用Java的HttpURLConnection类或HttpClient库来实现。下面是通过HttpURLConnection发送POST请求的示例代码: ```java import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class XWWPostRequestExample { public static void main(String[] args) throws Exception { // 设置请求URL URL url = new URL("http://www.example.com/api"); // 创建连接 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // 设置请求方法为POST connection.setRequestMethod("POST"); // 设置请求头 connection.setRequestProperty("Content-Type", "application/json"); connection.setRequestProperty("User-Agent", "Mozilla/5.0"); // 启用输出流 connection.setDoOutput(true); // 创建请求体 String requestBody = "{\"param1\":\"value1\",\"param2\":\"value2\"}"; // 将请求体写入输出流 DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream()); outputStream.writeBytes(requestBody); outputStream.flush(); outputStream.close(); // 获取响应状态码 int responseCode = connection.getResponseCode(); // 读取响应内容 BufferedReader reader; if (responseCode == HttpURLConnection.HTTP_OK) { reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); } else { reader = new BufferedReader(new InputStreamReader(connection.getErrorStream())); } String line; StringBuilder response = new StringBuilder(); while ((line = reader.readLine()) != null) { response.append(line); } reader.close(); // 打印响应内容 System.out.println(response.toString()); } } ``` 您需要将上述代码中的URL替换为XWW的请求URL,并根据实际情况修改请求头和请求体。此代码将发送一个POST请求并打印响应内容。请确保您已经包含了Java的HTTP库,如HttpURLConnection或HttpClient库。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值