HTTP POST - "Already connected"

forreason

Mitglied
Sehr geehrte community, ich bin noch ganz am anfang meiner HTTP-Java java kenntnisse.
mal abgesehen davon, das der Code noch ziemlich durcheinander ist bekomme ich folgende fehlermeldung:
Code:
run:

Testing 1 - Send Http GET request

Sending 'GET' request to URL : https://companion.orerve.net/
Response Code : 200
response url: https://companion.orerve.net/user/login
response: (too long)
Exception in thread "main" java.net.ProtocolException: Can't reset method: already connected

Testing 2 - Send Http POST request
test
    at java.net.HttpURLConnection.setRequestMethod(HttpURLConnection.java:409)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.setRequestMethod(HttpsURLConnectionImpl.java:374)
    at httptest.Httptest.sendPost(Httptest.java:90)
    at httptest.Httptest.main(Httptest.java:41)
Java Result: 1

Wenn ich mein "POST" in der Main methode auskommentiere läuft es durch.
woran liegt das bzw was muss ich ändern?

hier mein Programmcode:
Java:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package httptest;

/**
 *
 * @author BechtJu
 */
import java.io.*;
import java.net.*;

public class Httptest {

    /**
     * @param args the command line arguments
     */
    private final String    USER_AGENT = "Mozilla/5.0 (iPhone; CPU iPhone OS 8_1 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Mobile/12B411",
                            url = "https://companion.orerve.net/",
                            proxy = "10.180.230.5",
                            port = "8080",
                            user = "****",
                            password = "****";
    private final Proxy     prox = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxy, Integer.parseInt(port)));
    Authenticator           proxyauthenticator = new Authenticator() {
                                public PasswordAuthentication getPasswordAuthentication() {
                                return (new PasswordAuthentication(user,
                                password.toCharArray()));
                            }
                        };
    
    public static void main(String[] args) throws Exception{
        Httptest http = new Httptest();
        HttpURLConnection con = http.createconnection();
                System.out.println("\nTesting 1 - Send Http GET request");
        http.sendGet(con);
 
        System.out.println("\nTesting 2 - Send Http POST request");
        http.sendPost(con);
                
                System.out.println("disconnect");
                con.disconnect();
    }
    private HttpURLConnection createconnection() throws Exception{
        Authenticator.setDefault(proxyauthenticator);
    URL obj = new URL(url);                
    HttpURLConnection con = (HttpURLConnection) obj.openConnection(prox);
        return con;
    }
    private String[] sendGet(HttpURLConnection con) throws Exception {               

        // optional default is GET
        con.setRequestMethod("GET");
 
        //add request header
        con.setRequestProperty("User-Agent", USER_AGENT);
 
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();
 
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
 
        
                
                String[] returnarray = {
                    String.valueOf(con.getResponseCode()),
                    con.getURL().toString(),
                    response.toString(),
                    con.toString()
                };
                //print result
                System.out.println("\nSending 'GET' request to URL : " + url);
        System.out.println("Response Code : " + returnarray[0]);
                System.out.println("response url: " + returnarray[1]);
                System.out.println("response: ");
        System.out.println(returnarray[2]);
                return returnarray;
    }
    // HTTP POST request
    private String[] sendPost(HttpURLConnection con) throws Exception {
                Authenticator.setDefault(proxyauthenticator);
        //add request header
        con.setRequestMethod("POST");
        con.setRequestProperty("email", "****");
                con.setRequestProperty("password", "****");
 
        // Send post request
        con.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.flush();
        wr.close();
 
        int responseCode = con.getResponseCode();
        System.out.println("\nSending 'POST' request to URL : " + url);
        System.out.println("Response Code : " + responseCode);
 
        BufferedReader in = new BufferedReader(
                new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();
 
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
 
        //print result
        System.out.println(response.toString());
                
                String[] returnarray ={
                    "spaceholder"
                };
                return returnarray;
    }
    
}

Vielen Dank für eure Rückmeldung.
 
Zuletzt bearbeitet:

Neue Themen


Zurück
Oben