Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 连接sharepoint时发现消息302_Java_Http_Sharepoint_Token - Fatal编程技术网

Java 连接sharepoint时发现消息302

Java 连接sharepoint时发现消息302,java,http,sharepoint,token,Java,Http,Sharepoint,Token,尝试连接到sharepoint时,我收到消息HTTP/1.1 302 Found 我下面的代码是由mirontoli编写并由nddipiazza修改的 我还向URLConnection添加了一个代理 Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)); // added // URLConnection uc = u.openConnection(proxy); // modi

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

我下面的代码是由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;
}
从这里调用:

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到底是如何工作的,这就是我问的原因

问题出在哪里?


谢谢。

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