比较Python和Java之间的HTTP Post(Jenkins 302/403响应代码) [英] compare HTTP Post between Python and Java (Jenkins 302/403 response code)

查看:85
本文介绍了比较Python和Java之间的HTTP Post(Jenkins 302/403响应代码)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的python代码,但无法在Java中运行 它也可以通过curl和postman起作用. 请帮助

I have a simple python code working but I am unable to get it to work in Java it also works via curl and postman. Please help

以下代码是python,非常简单明了.它返回200.

The following code is python and is fairly simple and straightforward. it returns 200.

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

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

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

返回200

下面的代码在Java中,但是我找不到在Java中执行此操作的简单明了的方法.

The following code is in java and I am unable to find a simple and straightforward way to do this in 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响应代码:: 302
POST请求不起作用

推荐答案

与Jenkins特质相比,这更多的是与Jenkins的特性有关.预期会出现302错误代码,Jenkins接受并完成所需的工作,并返回302.尽管我不知道python内部如何处理它(并调用两次?)并最终返回代码200

This was more to do with Jenkins idiosyncrasies than to do with java. The 302 error code was expected and Jenkins accepts and does the work required and returns with 302. Although I don't know how python deals with it internally (and calls two times?) and returns code 200 eventually

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

FYI if setInstanceFollowedRedirects is set to true, I get a 403

jenkins错误
这就是我的解决方法.发布给可能会遇到它的其他人.

This is how I went around it. Posting for others who might run into it.

 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响应代码:: 302
http://jenkinsurl1
POST响应代码:: 200

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

这篇关于比较Python和Java之间的HTTP Post(Jenkins 302/403响应代码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆