java HttpClient执行挂起,是否调用了API?

1rhkuytd  于 2024-01-05  发布在  Java
关注(0)|答案(1)|浏览(198)

我使用JAVA HttpClient登录到一个API
然后HttpClient在一个循环中执行并使用这个函数(checkZipFile),第一次执行后,它挂起在client.execute(post)

  1. private String checkZipFile(HttpClient client, File zipFile){
  2. String errCode = "";
  3. try {
  4. HttpPost post = new HttpPost(URL_API);
  5. MultipartEntityBuilder builder = MultipartEntityBuilder.create();
  6. builder.addTextBody("command", "ImportCustomMaster");
  7. builder.addTextBody("type", "csvZip");
  8. builder.addBinaryBody(
  9. "dataFile", zipFile);
  10. builder.addTextBody("encoding", "932");
  11. HttpEntity multipart = builder.build();
  12. post.setEntity(multipart);
  13. HttpResponse response = client.execute(post); //<-- IT HANGS HERE
  14. System.out.println("\nSending 'POST' request to URL : " + URL_API);
  15. System.out.println("Post parameters : " + "dataFile: " + zipFile.getName());
  16. System.out.println("Response Code : " + response.getStatusLine().getStatusCode());
  17. BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
  18. StringBuffer result = new StringBuffer();
  19. String line = "";
  20. while ((line = rd.readLine()) != null) {
  21. result.append(line);
  22. }
  23. try {
  24. DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
  25. DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
  26. Document doc = dBuilder.parse(new InputSource(new StringReader(result.toString())));
  27. doc.getDocumentElement().normalize();
  28. Node nNode = doc.getElementsByTagName("error").item(0);
  29. if (nNode.getNodeType() == Node.ELEMENT_NODE) {
  30. Element eElement = (Element) nNode;
  31. errCode = eElement.getElementsByTagName("code").item(0).getTextContent();
  32. System.out.println("Error Code : " + errCode);
  33. }
  34. } catch (Exception e) {
  35. e.printStackTrace();
  36. }
  37. }catch (ConnectException e) {
  38. System.out.println(e.getMessage());
  39. return "";
  40. } catch (Exception e) {
  41. e.printStackTrace();
  42. return "";
  43. }
  44. return errCode;
  45. }

字符串
我不知道为什么,但至少,是API调用,然后它坚持,因为API或我的程序?有没有一些工具来调查?

rqcrx0a6

rqcrx0a61#

挂起的是API,而不是应用程序。
尝试使用Postman或curl来检查API的响应时间。这可能是因为文件太大或网络连接太慢->尝试发送一个非常小的文件来检查。

相关问题