HttpClient調用.net發布的帶Windows NTML驗證的webservice

來源:互聯網
上載者:User

標籤:

 

使用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      * 介面host102      */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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.