JavaHTTPpost请求中某⼀个参数太长的问题公司项⽬碰到⼀个问题,是从⼀个项⽬post请求到另⼀个项⽬,由于其中的⼀个参数实在太长,会报如下错误。java.lang.IllegalArgumentException: Request header is too large
通过更改l的⽅式配置Tomcat。
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1"
redirectPort="8443"  maxPostSize="0" maxHttpHeaderSize ="102400"/>
<Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8089" protocol="HTTP/1.1"
redirectPort="8443" maxPostSize="3097152000"
maxHttpHeaderSize ="10240000"/>
由于不知名原因,该⽅法并未奏效。
通过该代码的⽅式解决。
public static String sendPostRequest(String urlStr, Map<String, Object> params)throws Exception{
StringBuilder postData =new StringBuilder();
for(Map.Entry<String,Object> param : Set()){
if(postData.length()!=0){
postData.append('&');
}
java valueof
postData.Key(),"UTF-8"));
postData.append('=');
postData.de(String.Value()),"UTF-8"));
}
byte[] postDataBytes = String().getBytes("UTF-8");
HttpURLConnection conn =(HttpURLConnection)(new URL(urlStr)).openConnection();
Map<String, String> headers =new HashMap<String, String>();
String userpass =另⼀个项⽬的参数1+":"+另⼀个项⽬的参数2;
String basicAuth ="Basic "+ Base64Str(userpass,"encode");
headers.put("Authorization", basicAuth);
Iterator<Entry<String, String>> it = Set().iterator();
while(it.hasNext()){
Entry<String, String> entry = it.next();
conn.Key(), Value());
}
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
conn.setConnectTimeout(3000);
conn.setDoOutput(true);
BufferedReader reader =new BufferedReader(new InputStream(),"UTF-8"));
StringBuffer document =new StringBuffer();
String line = null;
while((line = adLine())!= null){
document.append(line +" ");
}
reader.close();
String().trim();
}
参考博客:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。