发送多个发布请求后,jdk11 HttpClient返回400

时间:2019-03-25 06:56:55

标签: java

我使用jdk11 HttpClient发送来自文件的msg,并每行发送一次。但是大约40次。出现400 statusCode。我不知道为什么?谢谢你的建议

以下是我的出处:

  

SendMsgHttp.java

//ignorance other source
 public void sendData(String rawHex,String versionType){
        String json_data =  key_protocolType+versionType+ key_upLinkData+rawHex+key_reqTimestamp;
        System.out.println("send data:"+json_data);
        HttpRequest request =  httpRequestBuilder
                .timeout(Duration.ofSeconds(10))
                .header("Content-Type", "application/json")
                .header("Authorization","Basic Y21kdDpDbUR0MTIzJA==")
                .POST(HttpRequest.BodyPublishers.ofString(json_data))
                .build();
        try {
            HttpResponse response= client.send(request, HttpResponse.BodyHandlers.ofString());
            System.out.println(response.statusCode());
            atomicNum.addAndGet(1);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
  

ReadFileAndSendThread.java

public class ReadFileAndSendThread implements  Runnable{
    private SendMsgHttp sendMsgHttp ;
    private  File file;
    private CountDownLatch latch;
    public ReadFileAndSendThread( File file,SendMsgHttp sendMsgHttp,CountDownLatch latch){
        this.file = file;
        this.sendMsgHttp = sendMsgHttp;
        this.latch = latch;
    }
    @Override
    public void run() {
        try {
            readSingleHandle();
            latch.countDown();
        } catch (IOException e) {
            System.err.println("go on");
            e.printStackTrace();
        }
    }
    public void  readSingleHandle() throws IOException {
        InputStreamReader isr=new InputStreamReader(new FileInputStream(file), "UTF-8");
        BufferedReader br = new BufferedReader(isr);
StandardOpenOption.READ);
        String tempString;
        while ((tempString = br.readLine()) != null) {
            try {
                String[] strData = tempString.split("\\|");
                String rawData  = strData[3].trim();
                String versionType = strData[2].trim();
                if(null !=rawData&&!rawData.equals("")
                   && null !=versionType&&!versionType.equals("")){
                    sendMsgHttp.sendData(rawData,versionType);
                    Thread.sleep(1000);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        br.close();
    }

}
  

主要SendMain.java

public class SendMain {
    public static void main(String[] args)  {
        ExecutorService threadPool = new ThreadPoolExecutor(1, 25,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue());
        String path = new File("").getAbsolutePath()+"/export";//"/root/xzg/send/exportdata";
        File[] fs = new File(path).listFiles();
        final CountDownLatch latch = new CountDownLatch(fs.length);
        for(File file:fs){
            threadPool.execute(new ReadFileAndSendThread(file,
                    new SendMsgHttp("http://localhost:9997/xxx")
            ,latch));
        }
        threadPool.shutdown();
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("count nums:"+SendMsgHttp.getSendCount());
    }
}

以及类似的文件内容 enter image description here

最后我运行它。结果如下

//ok
response.statusCode:200
response.statusCode:200
response.statusCode:200
response.statusCode:200
response.statusCode:400
response.statusCode:400
response.statusCode:400
//always 400 ??? this wrong is happend

根据结果我发现前47个数据总是成功的,其余的都失败了,而且运行了很多次,结果都是一样的

然后我尝试发送返回“ response.statusCode:400”的数据,这样就可以了(response.statusCode:200)

怎么回事?需要您的帮助。再次感谢。

0 个答案:

没有答案