Java 如何实现POST(x-www-form-urlencoded)请求
更新时间:2021年10月11日 10:53:45 作者:Commander_Officer
这篇文章主要介绍了Java 实现POST(x-www-form-urlencoded)请求,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
Java POST(x-www-form-urlencoded)请求
平时都是喜欢用JSON,这种也是第一次。这两种的区别就是传递参数类型不一样。废话不多说,直接上代码
1、引入maven包
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
2、代码实现
try {
String postURL
PostMethod postMethod = null;
postMethod = new PostMethod(postURL) ;
postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8") ;
//参数设置,需要注意的就是里边不能传NULL,要传空字符串
NameValuePair[] data = {
new NameValuePair("startTime",""),
new NameValuePair("endTime","")
};
postMethod.setRequestBody(data);
org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient();
int response = httpClient.executeMethod(postMethod); // 执行POST方法
String result = postMethod.getResponseBodyAsString() ;
return result;
} catch (Exception e) {
logger.info("请求异常"+e.getMessage(),e);
throw new RuntimeException(e.getMessage());
}
3、POSTMAN参数组装

使用post 请求x-www-form-urlencoded格式数据
代码如下:
public String getMsg() {
String result = "";
try {
URL url = new URL("https://XXXX.cn/token");
//通过调用url.openConnection()来获得一个新的URLConnection对象,并且将其结果强制转换为HttpURLConnection.
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
//设置连接的超时值为30000毫秒,超时将抛出SocketTimeoutException异常
urlConnection.setConnectTimeout(30000);
//设置读取的超时值为30000毫秒,超时将抛出SocketTimeoutException异常
urlConnection.setReadTimeout(30000);
//将url连接用于输出,这样才能使用getOutputStream()。getOutputStream()返回的输出流用于传输数据
urlConnection.setDoOutput(true);
//设置通用请求属性为默认浏览器编码类型
urlConnection.setRequestProperty("content-type", "application/x-www-form-urlencoded");
//getOutputStream()返回的输出流,用于写入参数数据。
OutputStream outputStream = urlConnection.getOutputStream();
String content = "grant_type=password&app_key="+APP_KEY+"&app_secret="+APP_SECRET;
outputStream.write(content.getBytes());
outputStream.flush();
outputStream.close();
//此时将调用接口方法。getInputStream()返回的输入流可以读取返回的数据。
InputStream inputStream = urlConnection.getInputStream();
byte[] data = new byte[1024];
StringBuilder sb = new StringBuilder();
//inputStream每次就会将读取1024个byte到data中,当inputSteam中没有数据时,inputStream.read(data)值为-1
while (inputStream.read(data) != -1) {
String s = new String(data, Charset.forName("utf-8"));
sb.append(s);
}
result = sb.toString();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
java中@ConfigurationProperties失效的问题解决
在Java开发中,使用@ConfigurationProperties注解读取配置文件时,如果配置类中的属性设置为static,将无法正确读取配置值,本文就来介绍一下具体解决方法,感兴趣的可以了解一下2024-09-09
java并发编程工具类PriorityBlockingQueue优先级队列
这篇文章主要为大家介绍了java并发编程工具类PriorityBlockingQueue优先级队列的方法示例应用详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步2022-03-03
使用idea搭建spring项目,利用xml文件的形式进行配置方式
本文介绍了如何使用SpringIOC和SpringDI的思想开发一个打印机模拟程序,实现了灵活配置彩色墨盒或灰色墨盒以及打印页面大小的功能,通过创建接口和实现类,并在配置文件中进行依赖注入,实现了控制反转2024-11-11
SpringBoot中Mybatis + Druid 数据访问的详细过程
Spring Boot 底层都是采用 SpringData 的方式进行统一处理各种数据库,SpringData也是Spring中与SpringBoot、SpringCloud 等齐名的知名项目,下面看下SpringBoot Mybatis Druid数据访问的详细过程,感兴趣的朋友一起看看吧2021-11-11


最新评论