HttpURLConnection:连接重置

时间:2019-01-24 11:17:59

标签: java rest http

这是我的HttpURLConnection方法,

public  String testHCI() {
    String response = "";
    try {
        input = CPIConn.class.getClassLoader().getResourceAsStream("cpi.properties");
        if(input==null){
            return "Sorry, unable to find the properties file";
        }
        prop.load(input);
        String url = prop.getProperty("cpi.url");
        URL urlObj = new URL(url);
        // Get XSRF Token
        HttpURLConnection connection1 = (HttpURLConnection) urlObj.openConnection();
        String userpass = prop.getProperty("cpi.userpass");
        byte[] encodeBase64 = Base64.encodeBase64(userpass.getBytes());
        String pass = new String(encodeBase64);
        //String auth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes());
        System.err.println(pass);
        connection1.setConnectTimeout(500000);
        connection1.setReadTimeout(500000);
        connection1.setRequestMethod("GET");
        connection1.setRequestProperty("Authorization", "BASIC " + new String(encodeBase64));
        connection1.setRequestProperty("Content-Type", prop.getProperty("cpi.content-type"));
        connection1.setRequestProperty("Accept", prop.getProperty("cpi.acccept"));
        connection1.setRequestProperty("DataServiceVersion", prop.getProperty("cpi.dataServiceVersion"));
        connection1.setRequestProperty("X-Requested-With", prop.getProperty("cpi.X-Requested-With"));
        connection1.setRequestProperty("Accept-Encoding", prop.getProperty("cpi.cpi.accept-Encoding"));
        connection1.setRequestProperty("Accept-Charset", prop.getProperty("cpi.accept-Charset"));
        connection1.setRequestProperty("Connection", "keep-alive");
        connection1.setDoInput(true);
        connection1.setDoOutput(true);
        connection1.setUseCaches(false);

        String payload = "{\"billing_record_code\": \"13124\",\"bu_code\": \"H0001\",\"clinic_location\": \"\",\"hospital_username\": \"test\",\"requested_datetime\": \"\",\"interface_identifier\": \"HMS-IMS002\",\"hn\": \"01-18-000012\",\"en\": \"O01-18-018211\",\"patient_row_id\": \"12123\",\"encounter_row_id\": \"13121\"}";
        DataOutputStream dataOutputStream = new DataOutputStream(connection1.getOutputStream());
        dataOutputStream.write(payload.getBytes());
        dataOutputStream.flush();
        dataOutputStream.close();
        connection1.connect();
        System.err.println("Workflow Trigger Response Code :" + connection1.getResponseCode());
        System.err.println("57");
        response = getDataFromStream(connection1.getInputStream());
        System.err.println("Workflow Response :" + response);
        input.close();
    } catch (Exception e) {
        e.printStackTrace();
        System.err.println("Trigger FAILURE " + e.getMessage());
        return "Trigger FAILURE "+ e.getMessage();
    }
    return " Workflows triggered successfully with response";
}
private  String getDataFromStream(InputStream stream) throws IOException {
    StringBuffer dataBuffer = new StringBuffer();
    BufferedReader inStream = new BufferedReader(new InputStreamReader(stream));
    String data = "";
    while ((data = inStream.readLine()) != null) {
        dataBuffer.append(data);
    }
    inStream.close();
    return dataBuffer.toString();
}

这是我的HttpURLConnection方法,谁能帮助我找到为什么此方法引发Exception的原因? java.net.SocketException:连接重置
此方法是调用CPI​​服务,以读取文档。

响应为:Trigger FAILURE Connection reset
有人可以帮我找到异常发生的地方吗?
这是由Main方法执行时的完整堆栈跟踪

java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:210)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
at sun.security.ssl.InputRecord.read(InputRecord.java:503)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:983)
at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:940)
at sun.security.ssl.AppInputStream.read(AppInputStream.java:105)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:286)
at java.io.BufferedInputStream.read(BufferedInputStream.java:345)
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:735)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:678)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:706)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1587)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:347)
at com.bdms.service.serviceLocalImp.CPIConn.testHCI(CPIConn.java:68)
at com.bdms.service.serviceLocalImp.CPIConn.main(CPIConn.java:22)

触发器失败连接resetjava.net.SocketException:连接resetnull
PS:忽略缩进。
PS:本文不重复,问题发生的地方不同。谢谢
谢谢。

0 个答案:

没有答案