java httpurlconnection因连接重置而失败

时间:2014-05-29 00:39:32

标签: java httpurlconnection

我在java中开始编码,并尝试将现有的.NET应用程序转换为连续的数据流。我尝试了下面的代码,但连接如果仍然是假的,虽然看起来连接没有数据被检索,然后它失败并重置连接。

private static HttpURLConnection getConnection(String urlString, String username, String password) throws IOException {
    URL url = new URL(urlString);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();



    connection.setReadTimeout(300000);
    connection.setConnectTimeout(30000);
    connection.setRequestProperty("Authorization", createAuthHeader(username, password));
    connection.setRequestProperty("Accept-Encoding", "gzip");
    connection.setRequestProperty("Content-Type","application/json");
    connection.setRequestProperty("Connection","Keep-Alive");
    connection.setRequestProperty("useragent", "Mozilla/5.0 (Windows; U; Windows 
           NT 6.0; en-US; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10 (.NET CLR 3.5.30729)");


    return connection;
    }
    ...

    public Producer(Properties cprops, String kafkaBroker, String kafkaTopic) {
....
    try {

        String charset = "UTF-8";
        String username = "me.com";
        String password = "password";
        String url = "https:/something.com.json";
        HttpURLConnection connection = null;
        InputStream inputStream = null;


        try {

            connection = getConnection(url, username, password);
            connection.connect();
            inputStream = connection.getInputStream();
            int responseCode = connection.getResponseCode();
            int numbytesRead = 0;

            if (responseCode >= 200 && responseCode <= 299) {
            BufferedReader reader = 
                                   new BufferedReader(new InputStreamReader
                                  (new StreamingGZIPInputStream(inputStream), charset));
                String line = reader.readLine();
                while(true){
                //read message per line and send to kafka
                numbytesRead = line.length();
                if (numbytesRead != 0) {   
                                    producer.send(new 
                                        KeyedMessage<String, String>(kafkaTopic,null,line));
                System.out.println(line);
                }else {
                  line = reader.readLine();
                  continue;
                }

编辑:添加了其他代码 任何帮助表示赞赏

0 个答案:

没有答案