0

I'm receiving the message HTTP/1.1 302 Found when making an attempt for connecting to sharepoint.

I'm following this code made by mirontoli and modified by nddipiazza.

Also I added a proxy to the URLConnection.

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

I believe is caused because the method extractToken() returns an empty String.

extractToken() code:

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;
}

Called from here:

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;
}

I don't know exactly how http works that is why I'm asking.

Which is the problem?

Thanks.

1 Answers1

0

when you get the return code 302, this is an information about a redirect. (see this wikipedia entry). You have to read the Location header from the response which contains the URL to which you are redirected an do a new calll this this address.

P.J.Meisch
  • 18,013
  • 6
  • 50
  • 66