我正在尝试将一些参数发布到外部网址,同时重定向到它。我尝试了httpclient方法,他们发布数据但不重定向到外部网址。 有任何想法吗? 这是我的代码: 试试{
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 :(得分:0)
您的问题风格通常不受欢迎。您应该向我们提供一些代码并告诉我们您尝试了什么,但是这个问题类似于我猜测您正在寻找的问题,并且我会附上接受的代码答案答案
Question Here 来自@alan geleynse的回答
String urlParameters = "param1=a¶m2=b¶m3=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();
答案 1 :(得分:0)
无法重定向HTTP POST请求。
10.3重定向3xx
此类状态代码表示用户代理需要采取进一步操作才能完成请求。当且仅当第二个请求中使用的方法是GET或HEAD时,所需的操作可以由用户代理执行而不与用户交互。
参考: