freeskycsdn 2013-07-03 14:57 采纳率: 100%
浏览 4749

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 2015-01-03 03:46
    关注
    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型