【问题标题】:Cookies in HttpUrlConnectionHttpUrlConnection 中的 Cookie
【发布时间】:2018-07-03 17:46:33
【问题描述】:

我制作了一个独立的应用程序来针对在线共享点进行身份验证,我设法获取了一个令牌和身份验证 cookie,但是,我在获取表单摘要值时遇到了问题,当我尝试从邮递员那里获取值时,我可以成功获取值但是,当我尝试从我的代码中获取值时,我解除了 java.io.IOException: Server returned HTTP response code: 411 for URL: https://XXXX.sharepoint.com/_api/contextinfo 我在谷歌上搜索问题并检查错误是否意味着需要 Context-Length 参数我添加了值 - 0 的此类参数,但结果是相同的。

如果有人遇到同样的问题,我将不胜感激 :) 这是我的代码

public String getFormDigestValue(List cookies) throws IOException {

    URL u = new URL("https://xxx.sharepoint.com/_api/contextinfo");
    URLConnection uc = u.openConnection();
    HttpURLConnection connection = (HttpURLConnection) uc;
    connection.setRequestMethod("POST");
    connection.setDoInput(true);
    connection.setDoOutput(true);
    connection.setInstanceFollowRedirects(false);
    connection.addRequestProperty("Accept", "application/x-www-form-urlencoded");
    connection.addRequestProperty("Content-Type","text/xml; charset=utf-8");
    connection.setRequestProperty("Cookie",cookies.get(2));
    connection.setRequestProperty("Cookie",cookies.get(1));
    connection.setRequestProperty("Content-Length","0");
    connection.setRequestProperty("Origin","xxx.sharepoint.com");
    connection.setDoOutput(true);

    InputStream in = connection.getInputStream();
    int c;
    StringBuilder sb = new StringBuilder("");
    while ((c = in.read()) != -1)
        sb.append((char) (c));
    in.close();
    String result = sb.toString();

    String startTag = "<d:FormDigestValue>";
    String endTag = "</d:FormDigestValue>";

    if(!(result.contains(startTag)) || !(result.contains(endTag))) {
        String errorMessage = "Error during getting form digest value. The form digest value is missing in the response of the request for getting form digest value.";
        throw new IOException(errorMessage);
    } else {
        int startIndex = result.indexOf(startTag, 1);
        int endIndex = result.indexOf(endTag, startIndex + 1);
        String formDigestValue = result.substring(startIndex + startTag.length(), endIndex);

        return formDigestValue;
    }


}

【问题讨论】:

    标签: java sharepoint


    【解决方案1】:

    我知道 5 个月前有人问过这个问题,但我为其他人提供了答案:

    构建您的帖子数据后:

    StringBuilder postData = new StringBuilder();
    ....
    ....
    

    在“Content-Length”属性中设置发布数据长度:

    postDataBytes = postData.toString().getBytes("UTF-8");
    connection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
    

    【讨论】:

      猜你喜欢
      • 2012-12-04
      • 1970-01-01
      • 2011-07-28
      • 1970-01-01
      • 2012-08-17
      • 2015-01-06
      • 1970-01-01
      • 1970-01-01
      • 2014-12-29
      相关资源
      最近更新 更多