从java后端发布表单并重定向到它

从java后端发布表单并重定向到它,java,Java,我试图在重定向到外部url时向其发布一些参数。我尝试过httpclient方法,它们发布数据,但不重定向到外部url。 有什么想法吗? 这是我的密码: 试一试{ 你的问题风格通常不受欢迎。你应该给我们一些代码并告诉我们你尝试了什么,但是这个问题与我猜你在寻找的问题类似,我会附上接受答案的代码答案 来自@alan geleynse的回答 String urlParameters = "param1=a&param2=b&param3=c"; String request = "h

我试图在重定向到外部url时向其发布一些参数。我尝试过httpclient方法,它们发布数据,但不重定向到外部url。 有什么想法吗? 这是我的密码: 试一试{


你的问题风格通常不受欢迎。你应该给我们一些代码并告诉我们你尝试了什么,但是这个问题与我猜你在寻找的问题类似,我会附上接受答案的代码答案

来自@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();

无法重定向HTTP POST请求

10.3重定向3xx

此类状态代码表示用户代理需要采取进一步的操作以满足请求。当且仅当第二个请求中使用的方法为GET或HEAD时,用户代理可以执行所需的操作,而不与用户交互

参考:


我已经用我的代码更新了问题。我想用参数作为post from backend重定向到外部站点。你的代码可以用于发布,但不能用于使用post重定向。这意味着任何来自后端代码的post方法都无法将其发送到操作页面?
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();