我在提交表单数据时使用POST作为方法(即textarea),但它仍然可以在URL中看到。这导致我的Web应用程序(在NetBeans上运行)出现以下错误:
Aug 30, 2016 11:25:02 AM org.apache.coyote.http11.AbstractHttp11Processor process
INFO: Error parsing HTTP request header
Note: further occurrences of HTTP header parsing errors will be logged at DEBUG level.
java.lang.IllegalArgumentException: Request header is too large
at org.apache.coyote.http11.InternalAprInputBuffer.fill(InternalAprInputBuffer.java:574)
at org.apache.coyote.http11.InternalAprInputBuffer.parseRequestLine(InternalAprInputBuffer.java:217)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:996)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:623)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2517)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2506)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:722)
这是由于URL中超出了限制(来自我收集的内容)。
index.jsp (代码片段):
form id="maryWebClient" action="preprocess" method="POST">
我后来尝试使用 Preprocessor.java 中的以下代码将该方法显式设置为POST,使用index.jsp(代码片段)中的urlPatterns向其发送数据:
URL obj=new URL("http://..../process?INPUT_TEXT="+"\""+ URLEncoder.encode(sent)+"\""+ "&INPUT_TYPE=TEXT&OUTPUT_TYPE=REALISED_DURATIONS&LOCALE=hi_IN&OUTPUT_TYPE_PARAMS=phone+stressed+accented");
BufferedReader bfr=new BufferedReader(new InputStreamReader(obj.openStream(),"utf8"));
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
答案 0 :(得分:0)
在POST请求中,参数在标题之后作为请求的主体发送。您正在传递URL中的参数,这使得它太大并且很可能是导致错误的原因。
URL obj=new URL("http://..../process?INPUT_TEXT="+"\""+ URLEncoder.encode(sent)+"\""+ "&INPUT_TYPE=TEXT&OUTPUT_TYPE=REALISED_DURATIONS&LOCALE=hi_IN&OUTPUT_TYPE_PARAMS=phone+stressed+accented");
打开连接后,您必须将数据写入HttpURLConnection。参数作为键值对写入请求主体。
按照Jekin Kalariya在答案中所写的内容,将所有数据写入连接。
String urlParameters = "INPUT_TEXT="+"\""+ URLEncoder.encode(sent)+"\""+ "&INPUT_TYPE=TEXT&OUTPUT_TYPE=REALISED_DURATIONS&LOCALE=hi_IN&OUTPUT_TYPE_PARAMS=phone+stressed+accented");
byte[] postData = urlParameters.getBytes( StandardCharsets.UTF_8 );
int postDataLength = postData.length;
String request = "http://..../process";
URL url = new URL( request );
HttpURLConnection conn= (HttpURLConnection) url.openConnection();
conn.setDoOutput( true );
conn.setInstanceFollowRedirects( false );
conn.setRequestMethod( "POST" );
conn.setRequestProperty( "Content-Type", "application/x-www-form- urlencoded");
conn.setRequestProperty( "charset", "utf-8");
conn.setRequestProperty( "Content-Length", Integer.toString( postDataLength ));
conn.setUseCaches( false );
try( DataOutputStream wr = new DataOutputStream( conn.getOutputStream())) {
wr.write( postData );
}
答案 1 :(得分:0)
以下方法解决了我的问题。我使用了ajax调用来发送帖子请求。
var postData=$("#maryWebClient").serializeArray();
$.ajax({
url: "preprocess",
context:this,
type:"POST",
dataType:"json",
data:postData,
success: function(response){
$("#INPUT_TEXT").val(response["x"]);
$("#INPUT_TEXT2").val(response["y"]);
},
error: function(errorData){alert(errorData);}
});
答案 2 :(得分:-1)
在GET请求中,参数作为URL的一部分发送。
在POST请求中,参数在标题之后作为请求的主体发送。
要使用HttpURLConnection进行POST,您需要在打开连接后将参数写入连接。
此代码可以帮助您入门:
String urlParameters = "param1=a¶m2=b¶m3=c";
byte[] postData = urlParameters.getBytes( StandardCharsets.UTF_8 );
int postDataLength = postData.length;
String request = "http://example.com/index.php";
URL url = new URL( request );
HttpURLConnection conn= (HttpURLConnection) url.openConnection();
conn.setDoOutput( true );
conn.setInstanceFollowRedirects( false );
conn.setRequestMethod( "POST" );
conn.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty( "charset", "utf-8");
conn.setRequestProperty( "Content-Length", Integer.toString( postDataLength ));
conn.setUseCaches( false );
try( DataOutputStream wr = new DataOutputStream( conn.getOutputStream())) {
wr.write( postData );
}