消息302连接sharepoint时找到

时间:2017-04-21 11:39:22

标签: java http sharepoint token

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

我关注了mirontoli制作并由nddipiazza修改的this代码。

我还在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是如何工作的,这就是我要问的原因。

问题是什么?

感谢。

1 个答案:

答案 0 :(得分:0)

当您收到返回代码302时,这是有关重定向的信息。 (见this wikipedia entry)。您必须从响应中读取位置标头,该标头包含您重定向到的URL,并对此地址执行新的调用。