从github读取存储库内容时获取Connect Exception

时间:2018-12-17 16:11:38

标签: java git github bitbucket

I am trying to read contents of a github repository. this is my code

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;

public class testInAction {

    public static void main(String[] args) throws Throwable {
String link = "https://stash.gto.intranet.db.com:8081/projects/PARAGON/repos/paragongit/browse";
        URL searchURL = new URL(link);
        System.out.println("start");
        HttpURLConnection searchHttp = (HttpURLConnection) searchURL.openConnection();
        Map<String, List<String>> searchHeader = searchHttp.getHeaderFields();
        InputStream searchStream = searchHttp.getInputStream();
        String searchResponse = searchGetStringFromStream(searchStream);
        System.out.println(searchResponse);
    }

    private static String searchGetStringFromStream(InputStream seachStream1) throws IOException {
        if (seachStream1 != null) {
            Writer searchWriter = new StringWriter();
            char[] searchBuffer = new char[2048];
            try {
                Reader searchReader = new BufferedReader(new InputStreamReader(seachStream1, "UTF-8"));
                int counter;
                while ((counter = searchReader.read(searchBuffer)) != -1) {
                    searchWriter.write(searchBuffer, 0, counter);
                }
            }

            finally {
                seachStream1.close();
            }
            return searchWriter.toString();
        } else {
            return "No Contents";
        }
    }
}
I am getting Connect Exception on running this. 

线程“ main”中的异常java.net.ConnectException:连接超时:connect     在sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)处     在sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)     在sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)     在java.lang.reflect.Constructor.newInstance(Constructor.java:423)     在sun.net.www.protocol.http.HttpURLConnection $ 10.run(HttpURLConnection.java:1890)     在sun.net.www.protocol.http.HttpURLConnection $ 10.run(HttpURLConnection.java:1885)     在java.security.AccessController.doPrivileged(本机方法)     在sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1884)     在sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1457)     在sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)     在sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)     在testInAction.main(testInAction.java:34)

0 个答案:

没有答案