在Unix中使用java访问UserProfile Web服务

时间:2015-10-30 07:15:39

标签: java web-services authentication sharepoint-2010

我在尝试通过unix环境中的java应用程序使用UserProfile Webservice时遇到问题。在Eclipse中测试时,完全相同的代码适用于Windows环境。但是在Centos 6.3操作系统中进行测试时,同样的抛出错误如下所示。

我已经从WSDL创建了Stub并使用它。基于Unix的服务器也未在域中注册。我确信它与用户凭据有关。但我不确定如何将其传递给应用程序。

Java主程序:

import com.microsoft.webservices.sharepointportalserver.userprofileservice.*;
import java.util.List;
import java.util.Properties;

public class TestWebservice{

    public static void main(String[] args) {

            UserProfileService ups = new UserProfileService();
            UserProfileServiceSoap upssoap = ups.getUserProfileServiceSoap();           
            ArrayOfPropertyData userInfo = upssoap.getUserProfileByName("E12345");          
            List<PropertyData> listPropertyData = userInfo.getPropertyData();

        for (int i=0; i < listPropertyData.size(); i++){
            PropertyData tempPropertyData = listPropertyData.get(i);
            System.out.println("tempPropertyData.getName = " + tempPropertyData.getName());
            ArrayOfValueData tempValues = tempPropertyData.getValues();

            List<ValueData> listValueData = tempValues.getValueData();
            System.out.println("listValueData.size()= "+listValueData.size());

            for (int j=0; j < listValueData.size(); j++){
                ValueData tempValueData = listValueData.get(j);
                Object aValue = tempValueData.getValue();
                System.out.println("aValue = " + aValue);
            }//end for j
        }//end for i

    }
}

UserProfileService.java(Stub)

private final static URL USERPROFILESERVICE_WSDL_LOCATION;
private final static WebServiceException USERPROFILESERVICE_EXCEPTION;
private final static QName USERPROFILESERVICE_QNAME = new QName("http://microsoft.com/webservices/SharePointPortalServer/UserProfileService", "UserProfileService");

static {
    URL url = null;
    WebServiceException e = null;
    try {
        url = new URL("http://SharepointServer/SiteName/_vti_bin/UserProfileService.asmx?WSDL");
    } catch (MalformedURLException ex) {
        e = new WebServiceException(ex);
    }
    USERPROFILESERVICE_WSDL_LOCATION = url;
    USERPROFILESERVICE_EXCEPTION = e;
}

public UserProfileService() {
    super(__getWsdlLocation(), USERPROFILESERVICE_QNAME);
}

public UserProfileService(WebServiceFeature... features) {
    super(__getWsdlLocation(), USERPROFILESERVICE_QNAME, features);
}

public UserProfileService(URL wsdlLocation) {
    super(wsdlLocation, USERPROFILESERVICE_QNAME);
}

public UserProfileService(URL wsdlLocation, WebServiceFeature... features) {
    super(wsdlLocation, USERPROFILESERVICE_QNAME, features);
}

public UserProfileService(URL wsdlLocation, QName serviceName) {
    super(wsdlLocation, serviceName);
}

public UserProfileService(URL wsdlLocation, QName serviceName, WebServiceFeature... features) {
    super(wsdlLocation, serviceName, features);
}

/**
 * 
 * @return
 *     returns UserProfileServiceSoap
 */
@WebEndpoint(name = "UserProfileServiceSoap")
public UserProfileServiceSoap getUserProfileServiceSoap() {
    return super.getPort(new QName("http://microsoft.com/webservices/SharePointPortalServer/UserProfileService", "UserProfileServiceSoap"), UserProfileServiceSoap.class);
}

/**
 * 
 * @param features
 *     A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy.  Supported features not in the <code>features</code> parameter will have their default values.
 * @return
 *     returns UserProfileServiceSoap
 */
@WebEndpoint(name = "UserProfileServiceSoap")
public UserProfileServiceSoap getUserProfileServiceSoap(WebServiceFeature... features) {
    return super.getPort(new QName("http://microsoft.com/webservices/SharePointPortalServer/UserProfileService", "UserProfileServiceSoap"), UserProfileServiceSoap.class, features);
}

private static URL __getWsdlLocation() {
    if (USERPROFILESERVICE_EXCEPTION!= null) {
        throw USERPROFILESERVICE_EXCEPTION;
    }
    return USERPROFILESERVICE_WSDL_LOCATION;
}

Unix中的错误:

Exception in thread "main" javax.xml.ws.WebServiceException: Failed to access the WSDL at: http://SharepointServer/SiteName/_vti_bin/UserProfileService.asmx?WSDL. It failed with: 
Got Server returned HTTP response code: 401 for URL: http://SharepointServer/SiteName/_vti_bin/UserProfileService.asmx?WSDL while opening stream from http://SharepointServer/SiteName/_vti_bin/UserProfileService.asmx?WSDL.
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.tryWithMex(RuntimeWSDLParser.java:173)
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parse(RuntimeWSDLParser.java:155)
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parse(RuntimeWSDLParser.java:120)
at com.sun.xml.internal.ws.client.WSServiceDelegate.parseWSDL(WSServiceDelegate.java:258)
at com.sun.xml.internal.ws.client.WSServiceDelegate.<init>(WSServiceDelegate.java:221)
at com.sun.xml.internal.ws.client.WSServiceDelegate.<init>(WSServiceDelegate.java:169)
at com.sun.xml.internal.ws.spi.ProviderImpl.createServiceDelegate(ProviderImpl.java:101)
at javax.xml.ws.Service.<init>(Service.java:77)
at com.microsoft.webservices.sharepointportalserver.userprofileservice.UserProfileService.<init>(UserProfileService.java:44)
at JacobClient.main(JacobClient.java:49)
Caused by: java.io.IOException: Got Server returned HTTP response code: 401 for URL: http://SharepointServer/SiteName/_vti_bin/UserProfileService.asmx?WSDL while opening stream from http://SharepointServer/SiteName/_vti_bin/UserProfileService.asmx?WSDL
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.createReader(RuntimeWSDLParser.java:842)
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.resolveWSDL(RuntimeWSDLParser.java:283)
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parse(RuntimeWSDLParser.java:140)
... 8 more
Caused by: java.io.IOException: Server returned HTTP response code: 401 for URL: http://myteamssgp1/sites/PTFApps/FARequest/_vti_bin/UserProfileService.asmx?WSDL
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1627)
at java.net.URL.openStream(URL.java:1041)
at     com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.createReader(RuntimeWSDLParser.java:827)
... 10 more

任何猜测需要什么才能让它在Unix服务器上运行

1 个答案:

答案 0 :(得分:0)

这段代码解决了我的问题

        Authenticator myAuth = new Authenticator() 
    {
        @Override
        protected PasswordAuthentication getPasswordAuthentication()
        {
            return new PasswordAuthentication("EMPID", "PASSWORD".toCharArray());
        }
    };