@@ -449,24 +449,34 @@ public static boolean checkIfProjectExists(String gitToken, String projectPath)
449449 public static String getUserIdByUsername (String gitUrl , String gitToken , String username ) throws GitErrorException , IOException {
450450 String url = gitUrl + "/api/v4/users?username=" + username ;
451451 BufferedReader in = null ;
452+ boolean useEntityUtils = GitServerConfig .GIT_USE_ENTITY_UTILS_FOR_RESPONSE .getValue ();
453+
452454 try (CloseableHttpClient httpClient = HttpClients .createDefault ()) {
453455 HttpGet request = new HttpGet (url );
454456 request .addHeader ("PRIVATE-TOKEN" , gitToken );
455457
456458 try (CloseableHttpResponse response = httpClient .execute (request )) {
457459 int statusCode = response .getStatusLine ().getStatusCode ();
458460 if (statusCode == 200 ) {
459- in = new BufferedReader (new InputStreamReader (response .getEntity ().getContent ()));
460- String inputLine ;
461- StringBuilder content = new StringBuilder ();
461+ String responseBody ;
462+
463+ if (useEntityUtils ) {
464+ // 使用 EntityUtils.toString() 方式读取响应
465+ responseBody = EntityUtils .toString (response .getEntity ());
466+ logger .info ("Response Body (using EntityUtils): " + responseBody );
467+ } else {
468+ // 保留原有的手动读取流方式
469+ in = new BufferedReader (new InputStreamReader (response .getEntity ().getContent ()));
470+ String inputLine ;
471+ StringBuilder content = new StringBuilder ();
462472
463- while ((inputLine = in .readLine ()) != null ) {
464- content .append (inputLine );
473+ while ((inputLine = in .readLine ()) != null ) {
474+ content .append (inputLine );
475+ }
476+ responseBody = content .toString ();
477+ logger .info ("Response Body (using manual stream reading): " + responseBody );
465478 }
466479
467- String responseBody = content .toString ();
468- logger .info ("Response Body: " + responseBody );
469-
470480 JsonArray jsonArray = JsonParser .parseString (responseBody ).getAsJsonArray ();
471481
472482 if (jsonArray .size () > 0 ) {
@@ -479,6 +489,9 @@ public static String getUserIdByUsername(String gitUrl, String gitToken, String
479489 throw new GitErrorException (80109 , "获取userId失败,请检查编辑用户token是否过期或git服务是否正常" );
480490 }
481491 }
492+ } catch (org .apache .http .MalformedChunkCodingException e ) {
493+ logger .error ("HTTP chunked transfer encoding error: " + e .getMessage (), e );
494+ throw new GitErrorException (80109 , "HTTP响应解析错误,服务器返回的分块数据格式不正确" , e );
482495 } catch (Exception e ) {
483496 throw new GitErrorException (80109 , "获取该git用户Id失败,原因为" , e );
484497 } finally {
0 commit comments