post请求返回400错误但是postman却调用成功
遇到一个很奇怪的问题,做一个http调用其他系统api的功能,代码写完后在本地测试一切正常,但是部署到服务器后post请求全部都400错误了
怀疑是参数格式错了,然后在日志里面把URL和参数复制出来在postman中调用测试,发现居然成功调用
然后以为请求工具类有问题,又在本地跑起来一遍,发现又是一切正常,那么代码应该没有问题才对
因为postman成功了所以地址和参数应该也没有问题,那么测试后也发现参数只有格式错了才会返回400错误
那么也就是说我的参数在发送之后被改变了格式,(一番折腾后)最后讲视线放在了Tomcat上面,查找资料的过程中得知Tomcat的属性里面有一个MaxPostSize属性,这个属性设置为-1就可以了,他的作用就是限制post请求的参数大小,-1是不限制大小,如果没有自己添加一下
在conf/server.xml 文件中,在 标签中设置 maxPostSize="-1"
后来发现上面那个方法是错误的
正确的是换一个http工具类,用HTTPclient这种方式,代码如下,token可以自己换掉
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HttpUtil {
private static final Logger log = LoggerFactory.getLogger(HttpUtil.class);
/**
* 原生字符串发送get请求
*
* @param url
* @return
* @throws IOException
* @throws
*/
public static String doGet(String url, String token) {
log.info("url:"+url);
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader("Content-type", "application/json");
httpGet.setHeader("DataEncoding", "UTF-8");
httpGet.setHeader("token", token);
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000).setConnectionRequestTimeout(35000).setSocketTimeout(60000).build();
httpGet.setConfig(requestConfig);
CloseableHttpResponse httpResponse = null;
try {
httpResponse = httpClient.execute(httpGet);
HttpEntity entity = httpResponse.getEntity();
String result = EntityUtils.toString(entity);
log.info("result:"+result);
return result;
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
log.info("ClientProtocolException:"+e.getMessage(),e);
} catch (IOException e) {
// TODO Auto-generated catch block
log.info("IOException:"+e.getMessage(),e);
} finally {
if (httpResponse != null) {
try {
httpResponse.close();
} catch (IOException e) {
// TODO Auto-generated catch block
log.info("IOException:"+e.getMessage(),e);
}
}
if (null != httpClient) {
try {
httpClient.close();
} catch (IOException e) {
log.info("IOException:"+e.getMessage(),e);
}
}
}
return null;
}
/**
* 原生字符串发送post请求
*
* @param url
* @param jsonStr
* @return
* @throws IOException
*/
public static String doPost(String url, String token, String jsonStr) {
log.info("url:"+url);
log.info("params:"+jsonStr);
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000).setConnectionRequestTimeout(35000).setSocketTimeout(60000).build();
httpPost.setConfig(requestConfig);
// 首先Header部分需要设定字符集为:uft-8
httpPost.addHeader("Content-Type", "application/json;charset=utf-8");
// 此处也需要设定
httpPost.setHeader("Accept", "application/json,text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
httpPost.setHeader("Accept-Encoding", "gzip, deflate");
httpPost.setHeader("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
httpPost.setHeader("Connection", "keep-alive");
httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36");
httpPost.setHeader("token", token);
CloseableHttpResponse httpResponse = null;
try {
httpPost.setEntity(new StringEntity(jsonStr,"utf-8"));
httpResponse = httpClient.execute(httpPost);
HttpEntity entity = httpResponse.getEntity();
String result = EntityUtils.toString(entity);
log.info("result:"+result);
return result;
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
log.info("ClientProtocolException:"+e.getMessage(),e);
} catch (IOException e) {
// TODO Auto-generated catch block
log.info("IOException:"+e.getMessage(),e);
} finally {
if (httpResponse != null) {
try {
httpResponse.close();
} catch (IOException e) {
// TODO Auto-generated catch block
log.info("IOException:"+e.getMessage(),e);
}
}
if (null != httpClient) {
try {
httpClient.close();
} catch (IOException e) {
log.info("IOException:"+e.getMessage(),e);
}
}
}
return null;
}
/**
* 原生字符串发送put请求
*
* @param url
* @param token
* @param jsonStr
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public static String doPut(String url, String token, String jsonStr) {
log.info("url:"+url);
log.info("params:"+jsonStr);
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPut httpPut = new HttpPut(url);
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000).setConnectionRequestTimeout(35000).setSocketTimeout(60000).build();
httpPut.setConfig(requestConfig);
// 首先Header部分需要设定字符集为:uft-8
httpPut.addHeader("Content-Type", "application/json;charset=utf-8");
// 此处也需要设定
httpPut.setHeader("Accept", "application/json,text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
httpPut.setHeader("Accept-Encoding", "gzip, deflate");
httpPut.setHeader("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
httpPut.setHeader("Connection", "keep-alive");
httpPut.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36");
httpPut.setHeader("token", token);
CloseableHttpResponse httpResponse = null;
try {
httpPut.setEntity(new StringEntity(jsonStr,"utf-8"));
httpResponse = httpClient.execute(httpPut);
HttpEntity entity = httpResponse.getEntity();
String result = EntityUtils.toString(entity);
log.info("result:"+result);
return result;
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
log.info("ClientProtocolException:"+e.getMessage(),e);
} catch (IOException e) {
// TODO Auto-generated catch block
log.info("IOException:"+e.getMessage(),e);
} finally {
if (httpResponse != null) {
try {
httpResponse.close();
} catch (IOException e) {
// TODO Auto-generated catch block
log.info("IOException:"+e.getMessage(),e);
}
}
if (null != httpClient) {
try {
httpClient.close();
} catch (IOException e) {
log.info("IOException:"+e.getMessage(),e);
}
}
}
return null;
}
/**
* 发送delete请求
*
* @param url
* @param token
* @param jsonStr
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public static String doDelete(String url, String token, String jsonStr) {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpDelete httpDelete = new HttpDelete(url);
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000).setConnectionRequestTimeout(35000).setSocketTimeout(60000).build();
httpDelete.setConfig(requestConfig);
httpDelete.setHeader("Content-type", "application/json");
httpDelete.setHeader("DataEncoding", "UTF-8");
httpDelete.setHeader("token", token);
CloseableHttpResponse httpResponse = null;
try {
httpResponse = httpClient.execute(httpDelete);
HttpEntity entity = httpResponse.getEntity();
String result = EntityUtils.toString(entity);
return result;
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (httpResponse != null) {
try {
httpResponse.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (null != httpClient) {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
}
6309




被折叠的 条评论
为什么被折叠?



