2 freeskycsdn freeskycsdn 于 2013.07.03 22:57 提问

RESTful API在代码中调用出现 505错误

问题如下:
1.RESTful API在浏览器中访问,没有问题,返回结果正确。
2.在代码里调这个RESTful API就会报505错误。

调用的代码结构如下:
1.先鉴权,并保存cookie
final String userPassword = bean.getUsername() + ":" + bean.getPassword();
final String encoding = new sun.misc.BASE64Encoder().encode(userPassword.getBytes());
String ssoCookieValue = null;
try {
final URL url = new URL(bean.getServerURL() + "authentication-point/authenticate");
final HttpURLConnection conn = (HttpURLConnection)url.openConnection();
// add the timeout to the connection and read
this.setTimeout(conn);
// set the basic authentication
conn.setRequestProperty("Authorization", "Basic " + encoding);

        // do get
        conn.connect();

        // try to authenticate the qc user and password
        final int responseCode = conn.getResponseCode();
        if (responseCode != HttpURLConnection.HTTP_OK) {
            if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
                throw new RESTException(Constants.EXCEPTION_AUTH_FAIL, new String[] {});
            } else if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
                throw new RESTException(Constants.EXCEPTION_NETWORK_FAIL, new String[] {});
            } else {
                log.error("*** response code is " + responseCode);

                throw new RESTException(Constants.EXCEPTION_NETWORK_FAIL, new String[] {});   
            }
        }

        // get return cookie
        ssoCookieValue = this.getCookieValue(conn, "LWSSO_COOKIE_KEY");
        log.debug("Cookie for LWSSO_COOKIE_KEY: " + ssoCookieValue);

        // disconnect
        conn.disconnect();
    } catch (IOException e) {
        if (e instanceof java.net.UnknownHostException) {
            throw new RESTException(Constants.EXCEPTION_UNKNOWN_HOST, new String[] {});
        } else if (e instanceof java.net.SocketTimeoutException) {
            throw new RESTException(Constants.EXCEPTION_CONNECTION_TIMEOUT, new String[] {});
        }

    }

2.然后构造connection,链接server,发出请求

    final URL url = new URL(this.buildRequestURL(bean, entityId));
    final HttpURLConnection conn = (HttpURLConnection)url.openConnection();
    // add the timeout to the connection and read
    this.setTimeout(conn);
    this.buildHeader(conn, ssoCookieValue);
    this.buildBody(conn, qc, entityId);
    conn.connect();

3.解析response

    final int responseCode = conn.getResponseCode();
    final JAXBContext jaxb = JAXBContext.newInstance("com.mycompany.myproject.jaxb");
    final Unmarshaller unmarshall = jaxb.createUnmarshaller();
    Object result = null;
    if (this.isSuccessful(responseCode)) {
        result = this.handleSuccessfulResponse(unmarshall, conn);
    } else {
        this.handleFailedResponse(responseCode, unmarshall, conn);
    }
    this.setResult(result);

问题就出在第三步的第一句
final int responseCode = conn.getResponseCode();

debug到这里,看到responseCode是505.

上网搜了一下,有人说505错误是不支持HTTP/1.1造成的,解决办法是取消IE中的“使用HTTP1.1”。
我这里的IE即使勾选上“使用HTTP1.1”,调用这个RESTful API也不会出错。
为什么在代码里就会有505错误呢?

请大家指点。谢谢!

1个回答

oyljerry
oyljerry   Ds   Rxr 2015.01.03 11:46
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!