将网址连接到主机

时间:2017-06-24 07:39:10

标签: java xmlrpcclient

使用xmlrpc连接到网络的指定网址。在这里我使用Apache库

  

线程中的异常" main" java.lang.IllegalStateException:connect in   进步   sun.net.www.protocol.http.HttpURLConnection.setRequestMethod(HttpURLConnection.java:550)     在xmlrpc.project.main(project.java:36)

当我运行java程序时发生此错误。请尝试给出一个ides来纠正错误。

public class project {
    final static String url ;

    public static void main(String args[]) throws XmlRpcException, IOException{
        final XmlRpcClient client = new XmlRpcClient();

        URL url1 = new URL(url);
        URLConnection urlConnection = url1.openConnection();
        InputStream is;
        if (((HttpURLConnection) urlConnection).getResponseCode() == 400) {
            is = urlConnection.getInputStream();
            System.out.println(is);
        } else {
            is = ((HttpURLConnection) urlConnection).getErrorStream();
        }
        ((HttpURLConnection) urlConnection).setRequestMethod("GET");
        urlConnection.setDoOutput(true);
        urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5");
        urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        urlConnection.setRequestProperty("Accept", "*/*");
        ((HttpURLConnection) urlConnection).setRequestMethod("POST");
        ((HttpURLConnection) urlConnection).setInstanceFollowRedirects(false); // very important line :)
        BufferedReader reader = new BufferedReader(new InputStreamReader(((HttpURLConnection) (new URL(url)).openConnection()).getInputStream(), Charset.forName("UTF-8")));
        @SuppressWarnings("unused")
        String temp="";
        while ( (temp = reader.readLine()) != null ) {}
        @SuppressWarnings("unused")
        String cookie = urlConnection.getHeaderField("Set-Cookie");

       /* HttpURLConnection connection = null;
        HttpURLConnection httpConn = (HttpURLConnection)connection;
        InputStream is;
        if (httpConn.getResponseCode() == 400) {
            is = httpConn.getInputStream();
            System.out.println(is);
        } else {
            is = httpConn.getErrorStream();
        }*/

        final XmlRpcClientConfigImpl start_config = new XmlRpcClientConfigImpl();
        start_config.setServerURL(new URL(url));

        final Map<String, String> info = (Map<String, String>)client.execute(
                start_config, "start", emptyList());

        final String url = info.get("host"),
                db = info.get("database"),
                username = info.get("user"),
                password = info.get("password");

        final XmlRpcClientConfigImpl common_config = new XmlRpcClientConfigImpl();
        common_config.setServerURL(
                new URL(String.format("%s/xmlrpc/2/common", url)));
        client.execute(common_config, "version", emptyList());
        int uid = (int)client.execute(
                common_config, "authenticate", asList(
                        db, username, password, emptyMap()));

        System.out.println(uid);
        if(uid>0){
            System.out.println("Successfully");
            }
        else{
            System.out.println("Failure");
            }


        }

        private static Object[] asList(String db2, String username2, String password2, Object emptyMap) {
            // TODO Auto-generated method stub
            return null;
        }

        private static Object emptyMap() {
            // TODO Auto-generated method stub
            return null;
        }

        private static Object[] emptyList() {
            // TODO Auto-generated method stub
            return null;
        }

    }

0 个答案:

没有答案