使用JAVA发送post请求,一个试了两次,一次是400一次是415
这个是415
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
这个是400
httpURLConnection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
package com.bulldozer.utils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
public class HttpUtil {
// 表示服务器端的url
private static String PATH = "http://8.140.147.123:9090/v1/refuse-treatment-plant";
private static URL url;
public HttpUtil() {
// TODO Auto-generated constructor stub
}
static {
try {
url = new URL(PATH);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*
* params 填写的URL的参数 encode 字节编码
*/
public static String sendPostMessage(Map<String, String> params,
String encode) {
StringBuffer stringBuffer = new StringBuffer();
if (params != null && !params.isEmpty()) {
for (Map.Entry<String, String> entry : params.entrySet()) {
try {
stringBuffer
.append(entry.getKey())
.append("=")
.append(URLEncoder.encode(entry.getValue(), encode))
.append("&");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 删掉最后一个 & 字符
stringBuffer.deleteCharAt(stringBuffer.length() - 1);
System.out.println("-->>" + stringBuffer.toString());
try {
HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
httpURLConnection.setConnectTimeout(3000);
httpURLConnection.setDoInput(true);// 从服务器获取数据
httpURLConnection.setDoOutput(true);// 向服务器写入数据
// 获得上传信息的字节大小及长度
byte[] mydata = stringBuffer.toString().getBytes();
// 设置请求体的类型
// httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpURLConnection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
httpURLConnection.setRequestProperty("Content-Lenth",
String.valueOf(mydata.length));
// 获得输出流,向服务器输出数据
OutputStream outputStream = (OutputStream) httpURLConnection
.getOutputStream();
outputStream.write(mydata);
// 获得服务器响应的结果和状态码
int responseCode = httpURLConnection.getResponseCode();
System.out.println("responseCode == " + responseCode);
if (responseCode == 200) {
// 获得输入流,从服务器端获得数据
InputStream inputStream = (InputStream) httpURLConnection
.getInputStream();
return (changeInputStream(inputStream, encode));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return "";
}
/*
* // 把从输入流InputStream按指定编码格式encode变成字符串String
*/
public static String changeInputStream(InputStream inputStream,
String encode) {
// ByteArrayOutputStream 一般叫做内存流
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] data = new byte[1024];
int len = 0;
String result = "";
if (inputStream != null) {
try {
while ((len = inputStream.read(data)) != -1) {
byteArrayOutputStream.write(data, 0, len);
}
result = new String(byteArrayOutputStream.toByteArray(), encode);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return result;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Map<String, String> params = new HashMap<String, String>();
params.put("licenseNumber", "FTYYUTYA");
params.put("plantName", "中国");
String result = sendPostMessage(params, "utf-8");
System.out.println("-result->>" + result);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
415(不支持的媒体类型),post请求 需要设置请求头
400指的是请求无效(请求有语法问题或者不能满足请求)
解决办法:对传递的数据进行json序列
这个415和400的话,你只是贴你的客户端代码是没用的,但是也可以从错误里面猜出个大概
415是说服务端不接受你的媒体格式,也就是application/x-www-form-urlencoded
这说明你不能用application/x-www-form-urlencoded了
用application/json; charset=utf-8返回400,说明服务端接受了你的请求,但是处理错误了,然后看你的代码,你发的还是form表单类的数据,你可以尝试把数据已json字符串发送,看看服务端是否正常处理
建议不要把实际环境的请求地址暴露出来,我点了一下竟然能返回数据。。。