【问题标题】:Submitted form data visible in url despite using method=post尽管使用 method=post,但提交的表单数据在 url 中可见
【发布时间】:2017-01-07 12:40:24
【问题描述】:

我在提交表单数据(即textarea)时使用了POST作为方法,但在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");

【问题讨论】:

    标签: java jsp http url post


    【解决方案1】:

    以下方法解决了我的问题。我已经使用 ajax 调用来发送一个 post 请求。

    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】:

      在 GET 请求中,参数作为 URL 的一部分发送。

      在 POST 请求中,参数作为请求正文发送,位于标头之后。

      要使用 HttpURLConnection 进行 POST,您需要在打开连接后将参数写入连接。

      此代码应该可以帮助您入门:

      String urlParameters  = "param1=a&param2=b&param3=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 );
      }
      

      【讨论】:

      • 感谢@Jenkin 的回答。我确实尝试了与此链接link 中提到的类似的方法。但这对我没有用。我也试过你的代码,但我得到了同样的错误。
      • 我很困惑为什么 jsp/HTML 表单 POST 会通过 servlet 路由?
      • @Scary 我专注于如何使用java URL类发送POST数据,其余方法取决于OP的观点,我无法理解与此相关的downvote
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-19
      • 1970-01-01
      • 1970-01-01
      • 2012-03-08
      • 2014-04-18
      • 2015-07-31
      • 1970-01-01
      相关资源
      最近更新 更多