0

while consuming a windows authenticated WebService from Java client -Eclipse, I 'm getting an error

    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)

Here is my Code though not in very good shape. Let me know how can i resolve this error. Also if there are any further links to where I'm,Please suggest.

    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;
            }
         }
    }
Pulkit Bhatia
  • 127
  • 1
  • 7
  • 22

2 Answers2

0

Try with running you app with "-Djava.net.preferIPv4Stack=true", see what it does

Mehul Rathod
  • 1,244
  • 8
  • 7
0

This is a simple working code for ntlm+httpclient i sure it will give you some basic idea to configure 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));