【问题标题】:HttpClient execute but hangs, is API called?HttpClient 执行但挂起,是否调用了 API?
【发布时间】:2020-02-12 07:29:28
【问题描述】:

我使用 JAVA HttpClient 登录 API

然后HttpClient循环执行并使用这个函数(checkZipFile),第一次之后就挂在“client.execute(post)”了。

private String checkZipFile(HttpClient client, File zipFile){
        String errCode = "";
        try {
            HttpPost post = new HttpPost(URL_API);

            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            builder.addTextBody("command", "ImportCustomMaster");
            builder.addTextBody("type", "csvZip");
            builder.addBinaryBody(
              "dataFile", zipFile);
            builder.addTextBody("encoding", "932");

            HttpEntity multipart = builder.build();

            post.setEntity(multipart);
            HttpResponse response = client.execute(post); //<-- IT HANGS HERE
            System.out.println("\nSending 'POST' request to URL : " + URL_API);
            System.out.println("Post parameters : " + "dataFile: " + zipFile.getName());
            System.out.println("Response Code : " + response.getStatusLine().getStatusCode());

            BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            StringBuffer result = new StringBuffer();
            String line = "";
            while ((line = rd.readLine()) != null) {
                result.append(line);
            }
            try {
                DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
                Document doc = dBuilder.parse(new InputSource(new StringReader(result.toString())));
                doc.getDocumentElement().normalize();

                Node nNode = doc.getElementsByTagName("error").item(0);
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element) nNode;
                    errCode = eElement.getElementsByTagName("code").item(0).getTextContent();
                    System.out.println("Error Code : " + errCode);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }


        }catch (ConnectException e) {
            System.out.println(e.getMessage());
            return "";
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
        return errCode;
    }

我当然不知道为什么,但至少,API 被调用然后它因为 API 或我的程序而卡住了? 有什么工具可以调查吗?

【问题讨论】:

    标签: java api httpclient freeze


    【解决方案1】:

    API 挂起,而不是您的应用程序。

    尝试使用Postman 或 curl 来检查 API 的响应时间。这可能是因为文件过大或互联网连接速度慢 -> 请尝试发送一个非常小的文件来检查。

    【讨论】:

    • 谢谢,文件大小只有大约1KB。我应该关闭一些回应吗?直到它卡住的时候,httpClient才执行了2次,在第三次执行时卡住了。
    • 那就试试邮递员,一个接一个地执行 3 次。他们可能有节流,但这应该返回一个失败的响应,而不是挂起。无论如何,如果 Postman 失败了,您需要与他们核实一下,因为您无能为力。
    猜你喜欢
    • 1970-01-01
    • 2013-03-30
    • 2014-08-21
    • 2013-11-03
    • 2020-11-30
    • 1970-01-01
    • 2017-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多