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
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?