码迷,mamicode.com
首页 > Windows程序 > 详细

HttpClient调用.net发布的带Windows NTML验证的webservice

时间:2016-03-17 10:50:15      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

 

使用HttpClient调用:

先要通过Windows NTML验证,然后才能调用 。

  1 class  WebServiceTest
  2 {
  3     
  4     /**
  5      * 获取接口数据
  6      * @param soapRequest
  7      * @return String
  8      */
  9     public String postSoapRequest(String soapRequest){
 10            CloseableHttpClient httpclient =  HttpClients.createDefault();
 11            //Windows NTLM验证
 12            CredentialsProvider credsProvider = new BasicCredentialsProvider();
 13            credsProvider.setCredentials(AuthScope.ANY,
 14                    new NTCredentials(InterfaceConstants.THE_USERNAME,
 15                            InterfaceConstants.THE_PASSWORD,
 16                            InterfaceConstants.THE_HOST, 
 17                            System.getenv("userdomain")));
 18            HttpHost target = new HttpHost(InterfaceConstants.THE_HOST, 80, "http");
 19            HttpClientContext context = HttpClientContext.create();
 20            context.setCredentialsProvider(credsProvider);
 21            HttpGet httpget = new HttpGet(InterfaceConstants.THE_URL);
 22            CloseableHttpResponse response1 = null;
 23            try {
 24                response1 = httpclient.execute(target, httpget, context);
 25            }catch(Exception e){
 26                e.printStackTrace();
 27            }finally {
 28                try {
 29                    response1.close();
 30                } catch (IOException e) {
 31                    e.printStackTrace();
 32                }
 33            }
 34            //使用相同的上下文,执行重量级的方法
 35            HttpPost httppost = new HttpPost(InterfaceConstants.THE_URL);
 36            HttpEntity re = new StringEntity(soapRequest, "utf-8");  
 37            httppost.setHeader("Content-Type","text/xml; charset=utf-8");
 38            httppost.setEntity(re);          
 39            CloseableHttpResponse response2 = null;
 40            String  result = null;//返回结果
 41            try {
 42                 response2 = httpclient.execute(target, httppost, context);
 43                 HttpEntity entity2 = response2.getEntity();
 44                 if (entity2 != null) {  
 45                    //响应内容  
 46                    result = EntityUtils.toString(entity2, "utf-8");  
 47                    System.out.println("isChunked:"+entity2.isChunked());
 48                 }  
 49            }catch(Exception e) {
 50                 e.printStackTrace();
 51            } finally {
 52                try {
 53                    response2.close();
 54                } catch (IOException e){
 55                    e.printStackTrace();
 56                }
 57            }
 58           return result;
 59     }
 60 
 61 
 62     @Test
 63     public void testHelloWorld()throws Exception {
 64         /*soap请求*/
 65         String soapRequest = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
 66                 +"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
 67                 +" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
 68                 +" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
 69                 +" <soap:Body>"
 70                 +"  <HelloWorld xmlns=\"http://tempuri.org/\"/>"
 71                 +"  </soap:Body>"
 72                 +"</soap:Envelope>";
 73         
 74         String res = postSoapRequest(soapRequest);//调用
 75 
 76         /*处理返回的结果*/
 77         Document document = DocumentHelper.parseText(res);
 78         Element root = document.getRootElement();
 79         Element body = root.element("Body");
 80         Element responseEle = body.element("HelloWorldResponse");
 81         Element resultEle = responseEle.element("HelloWorldResult");
 82         System.out.println(resultEle.getData());    
 83     
 84     }
 85 }
 86 
 87 
 88 /**接口相关信息*/
 89 public interface InterfaceConstants {
 90     
 91     public static final String THE_URL = "http://xxxxxxxxervice.asmx";  
 92     /**
 93      * 用户名
 94      */
 95     public static final String THE_USERNAME = "xxxxx";
 96     /**
 97      * 加密后密码
 98      */
 99     public static final String THE_PASSWORD = "xxxxx";    
100     /**
101      * 接口host
102      */
103     public static final String THE_HOST= "xxxxxxxxxxxxx";
104     /**
105      * 接口地址
106      */
107     public static final String THE_URL="http://xxxxxxxxervice.asmx";
108 
109 }

 

HttpClient调用.net发布的带Windows NTML验证的webservice

标签:

原文地址:http://www.cnblogs.com/davidxu/p/5286317.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!