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

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?