HttpClient调用.net发布的带Windows NTML验证的webservice
2016-03-17 10:31
841 查看
使用HttpClient调用:
先要通过Windows NTML验证,然后才能调用 。
class WebServiceTest { /** * 获取接口数据 * @param soapRequest * @return String */ public String postSoapRequest(String soapRequest){ CloseableHttpClient httpclient = HttpClients.createDefault(); //Windows NTLM验证 CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(AuthScope.ANY, new NTCredentials(InterfaceConstants.THE_USERNAME, InterfaceConstants.THE_PASSWORD, InterfaceConstants.THE_HOST, System.getenv("userdomain"))); HttpHost target = new HttpHost(InterfaceConstants.THE_HOST, 80, "http"); HttpClientContext context = HttpClientContext.create(); context.setCredentialsProvider(credsProvider); HttpGet httpget = new HttpGet(InterfaceConstants.THE_URL); CloseableHttpResponse response1 = null; try { response1 = httpclient.execute(target, httpget, context); }catch(Exception e){ e.printStackTrace(); }finally { try { response1.close(); } catch (IOException e) { e.printStackTrace(); } } //使用相同的上下文,执行重量级的方法 HttpPost httppost = new HttpPost(InterfaceConstants.THE_URL); HttpEntity re = new StringEntity(soapRequest, "utf-8"); httppost.setHeader("Content-Type","text/xml; charset=utf-8"); httppost.setEntity(re); CloseableHttpResponse response2 = null; String result = null;//返回结果 try { response2 = httpclient.execute(target, httppost, context); HttpEntity entity2 = response2.getEntity(); if (entity2 != null) { //响应内容 result = EntityUtils.toString(entity2, "utf-8"); System.out.println("isChunked:"+entity2.isChunked()); } }catch(Exception e) { e.printStackTrace(); } finally { try { response2.close(); } catch (IOException e){ e.printStackTrace(); } } return result; } @Test public void testHelloWorld()throws Exception { /*soap请求*/ String soapRequest = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " +" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +" <soap:Body>" +" <HelloWorld xmlns=\"http://tempuri.org/\"/>" +" </soap:Body>" +"</soap:Envelope>"; String res = postSoapRequest(soapRequest);//调用 /*处理返回的结果*/ Document document = DocumentHelper.parseText(res); Element root = document.getRootElement(); Element body = root.element("Body"); Element responseEle = body.element("HelloWorldResponse"); Element resultEle = responseEle.element("HelloWorldResult"); System.out.println(resultEle.getData()); } } /**接口相关信息*/ public interface InterfaceConstants { public static final String THE_URL = "http://xxxxxxxxervice.asmx"; /** * 用户名 */ public static final String THE_USERNAME = "xxxxx"; /** * 加密后密码 */ public static final String THE_PASSWORD = "xxxxx"; /** * 接口host */ public static final String THE_HOST= "xxxxxxxxxxxxx"; /** * 接口地址 */ public static final String THE_URL="http://xxxxxxxxervice.asmx"; }
相关文章推荐
- python网络爬虫入门(二)———模拟登陆知乎
- 我用HttpClient-MultipartEntity上传文字和图像 服务端没响应。
- Xcode 7.0 不能访问Http协议
- 判断网址的正则表达式 ((http|ftp|https)://)(([a-zA-Z0-9\\._-]+\\.[a-zA-Z]{2,6})|([0-9]{1,3}\\.[0-9]{1,3}\\.[0
- 简析TCP的三次握手与四次分手
- 飞行员配对方案问题 网络流||二分图匹配
- HttpClient 实现文件上传
- 判断网络是否可用
- 基本TCP套接字编程
- http协议、Request和Response、Cookie、HttpSession、JSP
- 数据库和Doctrine(转载自http://www.111cn.net/phper/332/85987.htm)
- TCP/IP ---分层
- HTTPS协议,SSL协议及完整交互过程
- HttpClient在Android Studio中报错
- tcp 学习网站
- tcp/ip、http、socket区别
- Swift - 网络请求报错App Transport Security has blocked a cleartext
- 深入浅出--iOS的TCP/IP协议族剖析&&Socket
- Java 网络编程(一)
- XMLHttpRequest Level 2 使用指南