【问题标题】:Message 302 Found while connecting sharepoint连接共享点时发现消息 302
【发布时间】:2017-09-18 09:39:17
【问题描述】:

我在尝试连接到 sharepoint 时收到消息 HTTP/1.1 302 Found

我正在关注 this 由 mirontoli 编写并由 nddipiazza 修改的代码。

我还向 URLConnection 添加了代理。

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)); // added //
URLConnection uc = u.openConnection(proxy); // modified //

我相信是因为方法extractToken()返回一个空字符串。

extractToken() 代码:

private String extractToken(String result)
        throws SAXException, IOException, ParserConfigurationException, XPathExpressionException {
    // http://stackoverflow.com/questions/773012/getting-xml-node-text-value-with-java-dom
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();

    Document document = db.parse(new InputSource(new StringReader(result)));

    XPathFactory xpf = XPathFactory.newInstance();
    XPath xp = xpf.newXPath();
    String token = xp.evaluate("//BinarySecurityToken/text()", document.getDocumentElement());
    // handle error S:Fault:
    // http://social.microsoft.com/Forums/en-US/crmdevelopment/thread/df862099-d9a1-40a4-b92e-a107af5d4ca2
    System.out.println(token);
    return token;
}

从这里调用:

private String requestToken()
        throws XPathExpressionException, SAXException, ParserConfigurationException, IOException {

    String saml = generateSAML();

    URL u = new URL(sts);
    Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));// added//
    URLConnection uc = u.openConnection(proxy);// modified//
    HttpURLConnection connection = (HttpURLConnection) uc;

    connection.setDoOutput(true);
    connection.setDoInput(true);
    connection.setRequestMethod("POST");
    // http://stackoverflow.com/questions/12294274/mobile-app-for-sharepoint/12295224#12295224
    // connection.addRequestProperty("SOAPAction", sts);
    connection.addRequestProperty("Content-Type", "text/xml; charset=utf-8");
    // connection.addRequestProperty("Expect", "100-continue");
    // connection.addRequestProperty("Connection", "Keep-Alive");
    // connection.addRequestProperty("Content-Length", saml.length() +
    // "");
    // connection.setRequestProperty("SOAPAction", SOAP_ACTION);

    OutputStream out = connection.getOutputStream();
    Writer wout = new OutputStreamWriter(out);
    wout.write(saml);

    wout.flush();
    wout.close();

    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 token = extractToken(result);
    System.out.println(token);
    return token;
}

我不知道 http 是如何工作的,这就是我问的原因。

问题出在哪里?

谢谢。

【问题讨论】:

    标签: java http sharepoint token


    【解决方案1】:

    当您获得返回码 302 时,这是关于重定向的信息。 (见this wikipedia entry)。您必须从包含您被重定向到的 URL 的响应中读取 Location 标头,然后重新调用此地址。

    【讨论】:

      猜你喜欢
      • 2021-10-20
      • 2014-01-26
      • 1970-01-01
      • 1970-01-01
      • 2018-12-20
      • 1970-01-01
      • 1970-01-01
      • 2014-02-21
      • 1970-01-01
      相关资源
      最近更新 更多