简体   繁体   English

IllegalStateException : 连接到 HttpURL 时已经连接

[英]IllegalStateException : Already Connected while connecting to a HttpURL

I am trying to read a git file by providing a http url of git repository.我正在尝试通过提供 git 存储库的 http url 来读取 git 文件。 My repository is not public so I provided the authentication details in my code and tried to connect but it is giving errors.我的存储库不是公开的,因此我在代码中提供了身份验证详细信息并尝试连接,但出现错误。 My code is as given below:我的代码如下:

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.net.URLConnection;
import java.util.List;
import java.util.Map;

import com.sun.xml.internal.messaging.saaj.util.Base64;

public class testInAction {

    static String username = "user@db.com";
    static String password = "12345678";

    public static void main(String[] args) throws Throwable {

        String link = "https://stash.gto.intranet.db.com:8081/projects/myproject/browse";
        URL searchURL = new URL(link);
        System.out.println("start");
        URLConnection searchHttp = (HttpURLConnection) searchURL.openConnection();
        searchHttp.setRequestProperty("X-Requested-With", "Curl");
        Map<String, List<String>> searchHeader = searchHttp.getHeaderFields();
        String userPass = username + ":" + password;
        String basicAuth = "Basic" + new String(new Base64().encode(userPass.getBytes()));
        searchHttp.setRequestProperty("Authorization", basicAuth);
        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";
        }
    }
}

But I am getting the IllegalStateException error on running this code.但是我在运行此代码时遇到了 IllegalStateException 错误。 The full error stack is as given below: Exception in thread "main" java.lang.IllegalStateException: Already connected at sun.net.www.protocol.http.HttpURLConnection.setRequestProperty(HttpURLConnection.java:3014) What can be the reason for this error and how to get it resolved?完整的错误堆栈如下:线程“main”中的异常 java.lang.IllegalStateException: Already connected at sun.net.www.protocol.http.HttpURLConnection.setRequestProperty(HttpURLConnection.java:3014) 可能是什么原因这个错误以及如何解决它?

From the javadoc :javadoc

URLConnection objects go through two phases: first they are created, then they are connected. URLConnection 对象经历两个阶段:首先创建它们,然后连接它们。 After being created, and before being connected, various options can be specified (eg, doInput and UseCaches).在创建之后和连接之前,可以指定各种选项(例如,doInput 和 UseCaches)。 After connecting, it is an error to try to set them.连接后,尝试设置它们是错误的。 Operations that depend on being connected, like getContentLength, will implicitly perform the connection, if necessary.如有必要,依赖于连接的操作(如 getContentLength)将隐式执行连接。

getHeaderFields() is one of those methods that will implicitly call connect() . getHeaderFields()是隐式调用connect() So you need to make sure to call it after the last call to setRequestProperty() .所以你需要确保在最后一次调用setRequestProperty()之后调用它。 Or just remove that line.或者只是删除该行。

Unrelated note: you're missing a space after Basic .不相关的说明:您在Basic之后缺少一个空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android:java.lang.IllegalStateException:已经连接 - Android: java.lang.IllegalStateException: Already connected 异常:java.lang.IllegalStateException:已连接 - Exception : java.lang.IllegalStateException: Already connected IllegalStateException:已与HttpURLConnection.setFixedLengthStreamingMode连接 - IllegalStateException: Already connected with HttpURLConnection.setFixedLengthStreamingMode HttpURLConnection.getRequestProperties()抛出IllegalStateException:“已经连接” - HttpURLConnection.getRequestProperties() throws IllegalStateException: “Already connected” java.lang.IllegalStateException:已在setDoOutput处连接 - java.lang.IllegalStateException: Already connected at setDoOutput 从Java与PHP连接时出现IllegalStateException - IllegalStateException while connecting with PHP from Java java.lang.IllegalStateException:已连接(Discord JDA) - java.lang.IllegalStateException: Already connected(Discord JDA) 线程“main”中的异常 java.lang.IllegalStateException:已连接 - Exception in thread “main” java.lang.IllegalStateException: Already connected Java AddRequestProperty已连接吗? - Java AddRequestProperty while already connected? IllegalStateException:抛出@WebFault时,“ WSEJBEndpointManager.ejbPostInvoke已被调用” - IllegalStateException: “WSEJBEndpointManager.ejbPostInvoke already called” while throwing @WebFault
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM