如何修复“ java.lang.IllegalStateException:已连接”

时间:2019-04-16 19:33:24

标签: java http

我正在尝试向API调用发布请求,但出现了“已连接”错误 我尝试删除标题,甚至是cookie,但始终显示“已连接” 在Visual Basic中,此逻辑完全相同。 我真的很惊讶它不能与Java一起使用 任何人都具有如何使用Java来invokevhttp发布请求的丰富知识吗?

通过调用帖子请求尝试连接到instagram API 而且我一直在错误以下

Caused by: java.lang.IllegalStateException: Already connected
    at sun.net.www.protocol.http.HttpURLConnection.setRequestProperty(HttpURLConnection.java:3014)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.setRequestProperty(HttpsURLConnectionImpl.java:316)
    at com.techline.instauserswitcher.FXMLDocumentController.replaceUserVb(FXMLDocumentController.java:154)
    at com.techline.instauserswitcher.FXMLDocumentController.replaceUser(FXMLDocumentController.java:113)
    at com.techline.instauserswitcher.FXMLDocumentController.searchByUser(FXMLDocumentController.java:106)
    at com.techline.instauserswitcher.FXMLDocumentController.handleStartButtonAction(FXMLDocumentController.java:82)
    ... 58 more

这是给出我想在下面修复的错误的方法

   private boolean replaceUserVb(String myVictim) throws Exception {
            System.out.println("inside replaceUserVb");
            boolean result = false;
            String string_11 = "5ad7d6f013666cc93c88fc8af940348bd067b68f0dce3c85122a923f4f74b251";
            String str = "signed_body=";
            StringBuilder text = new StringBuilder("signed_body=");
            text.append("{\"\"gender\"\":\"\"1\"");
            text.append("\"_csrftoken\"\":\"\"missing\"");
            text.append("\"_uuid\"\":\"\"");
            String guiid = randomAlphaNumeric(16);
            System.out.println("guiid >>"+ guiid);
            text.append(guiid);
            text.append("\"\",\"\"_uid\"\":\"\"3\"");
            text.append("\"external_url\"\":\"\"www.instagram.com/0k0k\"\",\"\"username\"\":\"\"");
            text.append(myVictim);
            text.append("\"");
            text.append("\"email\"\":\"\"");
            text.append("cssxereria@gmail.com");
            text.append("\"");
            text.append("\"phone_number\"\":\"\"\"");
            text.append("\"biography\"\":\"\"\"");
            text.append("\"first_name\"\":\"\"L0N3LY\"\"}");
            System.out.println("text >>" + text.toString());
            String mainText = text.toString();
            //encrypt mainText with key string_11)
            String str2 = sMethod_4(string_11, mainText);
            String str3 = "&ig_sig_key_version=5";

            URL sendUrl = new URL("https://i.instagram.com/api/v1/accounts/edit_profile/");
            HttpURLConnection httpConnection = (HttpURLConnection) sendUrl.openConnection();
    //        httpConnection.setDoInput(true);
            httpConnection.setRequestMethod("POST");
            httpConnection.setAllowUserInteraction(true);
            String cookiesHeader = httpConnection.getHeaderField("Set-Cookie");
            List<HttpCookie> cookies = HttpCookie.parse(cookiesHeader);
            httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            httpConnection.setRequestProperty("User-Agent", "Instagram 10.3.2 Android (18/4.3; 320dpi; 720x1280; Xiaomi; HM 1SW; armani; qcom; en_US)");
            httpConnection.setRequestProperty("User-Agent", "Instagram 7.3.1 Android (21/5.0.1; 480dpi; 1080x1920; samsung/Verizon; SCH-I545; jfltevzw; qcom; en_US)");
            httpConnection.setInstanceFollowRedirects(true);
            httpConnection.setConnectTimeout(7500);
            httpConnection.setReadTimeout(7500);
            httpConnection.setRequestProperty("X-IG-Connection-Type", "WIFI");
            httpConnection.setRequestProperty("X-IG-Capabilities", "3ToAAA==");

            httpConnection.setDoOutput(true);
    httpConnection.connect();
            DataOutputStream dataStreamToServer = new DataOutputStream(httpConnection.getOutputStream());
            dataStreamToServer.writeBytes(str + str2 + str3);
            dataStreamToServer.flush();
            dataStreamToServer.close();
    // Here take the output value of the server.
            BufferedReader dataStreamFromUrl = new BufferedReader(new InputStreamReader(httpConnection.getInputStream()));
            String dataFromUrl = "", dataBuffer = "";
    // Writing information from the stream to the buffer
            while ((dataBuffer = dataStreamFromUrl.readLine()) != null) {
                dataFromUrl += dataBuffer;
            }
            /**
             * Now dataFromUrl variable contains the Response received from the *
             * server so we can parse the response and process it accordingly.
             */
            dataStreamFromUrl.close();
            System.out.println("Response: " + dataFromUrl);

            System.out.println("After geting and closing httpconnection");
            result = true;
            return result;
        }

        public static String sMethod_4(String key, String mainText) throws Exception {
            System.out.println("inside sMethod_4");
            Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
            SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256");
            sha256_HMAC.init(secret_key);

            String output = Hex.encodeHexString(sha256_HMAC.doFinal(mainText.getBytes("UTF-8")));
            System.out.println("encrypted output is >>" + output);
            return output;
        }

        public static String randomAlphaNumeric(int count) {
            StringBuilder builder = new StringBuilder();
            while (count-- != 0) {
                int character = (int) (Math.random() * ALPHA_NUMERIC_STRING.length());
                builder.append(ALPHA_NUMERIC_STRING.charAt(character));
            }
            return builder.toString();
        }

0 个答案:

没有答案