比较 Python 和 Java 之间的 HTTP Post (Jenkins 302/403 响应代码)

compare HTTP Post between Python and Java (Jenkins 302/403 response code)

我有一个简单的 python 代码可以工作,但我无法让它在 Java 中工作 它也可以通过 curl 和邮递员工作。 请帮忙

下面的代码是python,相当简单明了。它 returns 200.

<!-- language: lang-python -->

import requests
params = (
    ('member', 'xxx'),
)

response = requests.post('http://jenkinsurl1/submitRemoveMember', params=params, auth=('user', 'notbase64encodedtoken'))
print(response)

Returns 200

以下代码在 java 中,我无法在 java 中找到一种简单直接的方法来执行此操作。

<!-- language: lang-java -->

//main() function

String auth = "user" + ":" + "notbase64encodedtoken";
byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes(StandardCharsets.UTF_8));
final String POST_PARAMS = "member=xxxx";
MyPOSTRequest(POST_PARAMS,encodedAuth,"http://jenkinsurl1/submitRemoveMember");


public static void MyPOSTRequest(String Parameters, byte[] encodedAuth, String POST_URL) throws IOException {

URL obj = new URL(POST_URL);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
byte[] postData       = Parameters.getBytes( StandardCharsets.UTF_8 );
int    postDataLength = postData.length;
con.setRequestMethod("POST");
con.setDoOutput( true );
con.setInstanceFollowRedirects( false );
con.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded");
con.setRequestProperty( "charset", "utf-8");
con.setRequestProperty( "Content-Length", Integer.toString( postDataLength ));
String authHeaderValue = "Basic " + new String(encodedAuth);
con.setRequestProperty("Authorization", authHeaderValue);
con.setUseCaches( false );
    
try( DataOutputStream wr = new DataOutputStream( con.getOutputStream())) {
    wr.write( postData );
    wr.flush();
}
int responseCode = con.getResponseCode();
System.out.println("POST Response Code :: " + responseCode);
    
if (responseCode == HttpURLConnection.HTTP_OK) { //success
  BufferedReader in = new BufferedReader(new InputStreamReader(
  con.getInputStream()));
  String inputLine;
  StringBuffer response = new StringBuffer();
    
   while ((inputLine = in.readLine()) != null) {
     response.append(inputLine);
    }
    in.close();
    
     // print result
     System.out.println(response.toString());
     } else {
       System.out.println("POST request not worked");
       }
 }

POST Response Code :: 302
POST request not worked

您的 Java 代码不处理重定向响应(HTTP 代码 302)。

这与 Jenkins 的特质有关,而不是 java。 302 错误代码是预期的,Jenkins 接受并完成所需的工作,returns 使用 302。虽然我不知道 python 如何在内部处理它(并调用两次?)和 returns 最终代码 200

仅供参考,如果 setInstanceFollowedRedirects 设置为 true,我会收到 403

jenkins bug
similar unanswered on Whosebug

我就是这样绕过它的。为可能 运行 加入其中的其他人发帖。

 int responseCode = con.getResponseCode();
 System.out.println("POST Response Code :: " + responseCode);

 if (responseCode == HttpURLConnection.HTTP_MOVED_TEMP) { //MOVED_TEMP
  String location = con.getHeaderField("Location");
  System.out.println(location);
  MyPOSTRequest(Parameters, encodedAuth, location); //calling the same function again with redirected url.
 }

POST Response Code :: 302
http://jenkinsurl1
POST Response Code :: 200