从java客户端使用.net webservice时java.net.UnknownHostException

时间:2013-07-24 09:49:22

标签: java .net eclipse web-services webservices-client

从Java客户端-Eclipse消耗Windows认证的WebService时,我收到错误

    at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
    at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:876)
    at java.net.InetAddress.getAddressFromNameService(InetAddress.java:1229)
    at java.net.InetAddress.getAllByName0(InetAddress.java:1180)
    at java.net.InetAddress.getAllByName(InetAddress.java:1110)
    at java.net.InetAddress.getAllByName(InetAddress.java:1046)

这是我的代码,虽然形状不是很好。让我知道如何解决此错误。如果还有任何进一步链接到我的位置,请建议。

    import com.microsoft.webservices.OfficeServer.QueryService.*;
    import java.net.InetAddress;
    import java.net.URL;
    import org.apache.axis.client.Stub;
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpHost;
    import org.apache.http.HttpResponse;
    import org.apache.http.auth.AuthScope;
    import org.apache.http.auth.NTCredentials;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.params.AuthPolicy;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.params.CoreProtocolPNames;
    import org.apache.http.params.HttpConnectionParams;
    import org.apache.http.protocol.BasicHttpContext;
    import org.apache.http.protocol.HttpContext;
    import org.apache.http.util.EntityUtils;

    public class ppConsume3
    {
    public static void main(String[] arg)
         {
            DefaultHttpClient httpclient = new DefaultHttpClient();
        httpclient.getAuthSchemes().register(AuthPolicy.NTLM, new NTLMSchemeFactory());
        httpclient.getCredentialsProvider().setCredentials(new AuthScope(null, 80), new NTCredentials("userName", "password", "http://delnshar881501/_vti_bin/search.asmx?WSDL", "SAPIENT"));
            HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), 5000); 
            HttpHost target = new HttpHost("http://delnshar881501/_vti_bin/search.asmx?WSDL",80);
            System.getProperties().setProperty("http.proxyUser ", "userName");
            System.getProperties().setProperty("http.proxyPass word", "password");
            // Make sure the same context is used to execute
            // logically related requests
            HttpContext localContext = new BasicHttpContext();

            String content;
            try
            {
                // Execute a cheap method first. This will trigger NTLM
                // authentication
                HttpGet httpget = new HttpGet();
                httpget.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
                httpclient.execute(target, httpget, localContext);

                // Execute an expensive method next reusing the same context
                HttpGet httppost = new HttpGet();
                httpget.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
                HttpResponse response2 = httpclient.execute(target, httppost, localContext);
                HttpEntity entity2 = response2.getEntity();

                content = EntityUtils.toString(entity2);
            }
            catch (Exception e)
            {
                e.printStackTrace();
                content = "<html><body><p>"+e.getMessage()+"</p></body></html>";        
                return;
            }
         }
    }

2 个答案:

答案 0 :(得分:0)

尝试使用“-Djava.net.preferIPv4Stack = true”运行应用程序,看看它做了什么

答案 1 :(得分:0)

这是一个简单的ntlm + httpclient工作代码我相信它会给你一些基本的想法来配置ntlm

        DefaultHttpClient httpclient = new DefaultHttpClient();
List<String> authpref = new ArrayList<String>();
authpref.add(AuthPolicy.NTLM);
httpclient.getParams().setParameter(AuthPNames.TARGET_AUTH_PREF, authpref);
NTCredentials creds = new NTCredentials("username", "pwd", "server", "domain");
httpclient.getCredentialsProvider().setCredentials(AuthScope.ANY, creds);

HttpHost target = new HttpHost("localhost", 80, "http");

// Make sure the same context is used to execute logically related requests
HttpContext localContext = new BasicHttpContext();

// Execute a cheap method first. This will trigger NTLM authentication
HttpGet httpget = new HttpGet("/index.html");
HttpResponse response = httpclient.execute(target, httpget, localContext);
HttpEntity entity = response.getEntity();
System.out.println(EntityUtils.toString(entity));