2014-08-30 28 views
-6

我想在重定向到某个外部URL时发送几个参数。我试过httpclient方法,他们发布的数据,但不重定向到外部网址。 有什么想法? 这里是我的代码: 尝试{从java后端发布表单并重定向到它

 HttpClient httpClient = new HttpClient(); 
     PostMethod postMethod = new PostMethod("https://paymentsite.com/pay") { 
      @Override 
      public boolean getFollowRedirects() { 
       System.out.println("Overrriding the HttpClient Follow Redirect switch."); 
       return true; 
      } 
     };; 
     try { 
      //httpClient 
      postMethod.addParameter("amt","850"); 
      postMethod.addParameter("pid","155"); 
      httpClient.executeMethod(postMethod); 

      if (postMethod.getStatusCode() == HttpStatus.SC_OK) { 
       //now i want the user to be redirected to the external site. with the post parameters... 

      } else { 
      } 
     } catch (Exception e) { 
      System.out.println("Exception : " + e.toString()); 
      e.printStackTrace(); 
     } 

    } catch (Exception e) { 
     System.out.println("exception main: " + e.toString()); 
     e.printStackTrace(); 
    } 

回答

0

HTTP POST请求无法重定向。

10.3重定向3XX

这个类的状态代码表示进一步的动作,才能完成请求将要采取的用户代理。当且仅当第二个请求中使用的方法是GET或HEAD时,所需的操作可以由用户代理执行,而不与用户交互。

参考:

+0

所以这意味着后端代码中的任何post方法都无法将其发送到操作页面? – hotplugin 2014-08-31 05:42:36

+0

这是正确的。 – 2014-08-31 06:10:35

0

你的问题样式通常令人难以接受的。你应该已经给了我们一些代码,并告诉我们你试过但是什么这个问题是相似,我猜你正在寻找的,我会附上接受的答案

Question Here 回答的答案代码from @alan geleynse

String urlParameters = "param1=a&param2=b&param3=c"; 
String request = "http://example.com/index.php"; 
URL url = new URL(request); 
HttpURLConnection connection = (HttpURLConnection) url.openConnection();   
connection.setDoOutput(true); 
connection.setDoInput(true); 
connection.setInstanceFollowRedirects(false); 
connection.setRequestMethod("POST"); 
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
connection.setRequestProperty("charset", "utf-8"); 
connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length)); 
connection.setUseCaches (false); 

DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); 
wr.writeBytes(urlParameters); 
wr.flush(); 
wr.close(); 
connection.disconnect(); 
+0

我已经与我的代码更新的问题。我想用参数作为后端从后端重定向到外部网站。您的代码适用于发布,但不适用于重定向帖子。 – hotplugin 2014-08-31 05:35:50

相关问题