当前位置:  开发笔记 > 编程语言 > 正文

Java HttpConnection拒绝,但卷曲等效工作

如何解决《JavaHttpConnection拒绝,但卷曲等效工作》经验,为你挑选了1个好方法。

我对这个失去了理智.我的curl命令有效:

curl http:// testuser:testpwd @ qabox3:8501/xmlcontroller

但是,当我尝试在Java中看起来像是等效的http连接时,它会"拒绝连接".我错过了什么?我已经尝试了十几种尝试今天建立这种联系的方式并且没有想法.

        URL url = new URL( "http://qabox3:8051/xmlcontroller" );
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.setRequestMethod( "GET" );
        String encoding = new sun.misc.BASE64Encoder().encode( "testuser:testpwd".getBytes() );
        conn.setRequestProperty("Authorization", "Basic " + encoding );
        InputStream content = conn.getInputStream();  // <--- fails here every time.
        BufferedReader in = new BufferedReader( new InputStreamReader( content ) );
        String line;
        while ((line = in.readLine()) != null) {
            System.out.println( line );
        }

此外,我可以使用Java的Runtime.exec()执行curl命令,但仍然有效......所以我在HttpURLConnection中显然做错了.

这是我看到的堆栈(现在使用HttpClient,但与Java库基本相同).

Exception in thread "main" java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:520)
at java.net.Socket.connect(Socket.java:470)
at java.net.Socket.(Socket.java:367)
at java.net.Socket.(Socket.java:240)
at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:80)
at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:122)
at org.apache.commons.httpclient.HttpConnection.open(HttpConnection.java:707)
at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:387)
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:323)
at MyClass.sendRequest(iQ411RequestHandlerProxy.java:277)

只是为了好玩,这里是卷曲详细的输出.标题中没什么特别之处......

> GET /xmlcontroller HTTP/1.1
> Authorization: Basic cWFfc3VwZXI6cWFfc3VwZXI=
> User-Agent: curl/7.16.3 (i686-pc-cygwin) libcurl/7.16.3 OpenSSL/0.9.8k zlib/1.2.3 libssh2/0.15-CVS
> Host: qabox3:8501
> Accept: */*
>

小智.. 11

您似乎使用两个不同的端口号:8501和8051?这可能是问题还是发布问题的错字?

curl http:// testuser:testpwd @ qabox3:8501/xmlcontroller

URL url = new URL(" http:// qabox3:8051/xmlcontroller ");

问候,伊恩



1> 小智..:

您似乎使用两个不同的端口号:8501和8051?这可能是问题还是发布问题的错字?

curl http:// testuser:testpwd @ qabox3:8501/xmlcontroller

URL url = new URL(" http:// qabox3:8051/xmlcontroller ");

问候,伊恩


只是表明让第二双眼睛看着你的代码永远不会伤害.好抓伊恩.
推荐阅读
贴进你的心聆听你的世界
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有