【问题标题】:java.net.UnknownHostException while consuming .net webservice from java clientjava.net.UnknownHostException 从 java 客户端使用 .net webservice
【发布时间】:2013-07-24 09:49:21
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    尝试使用“-Djava.net.preferIPv4Stack=true”运行您的应用,看看它的作用

    【讨论】:

      【解决方案2】:

      这是一个简单的 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));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-16
        • 1970-01-01
        • 1970-01-01
        • 2012-04-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多