zoukankan      html  css  js  c++  java
  • So easy Webservice 3.使用HttpClient工具调用Webservice接口

    首先,看看webservice服务调用演示:

    a) 登录http://www.webxml.com.cn

    b) 单击手机查询服务

    c) 选择要调用的方法 例如: getMobileCodeInfo

    输入要查询的手机号单击”调用” 截图如下, 免费用户 UserID为null

    返回结果:

    HttpClient工具调用WS服务接口

    HttpClient使用步骤如下:

    1. 创建 HttpClient 的实例 
    2. 创建某种连接方法的实例,在这里是 GetMethod。在 GetMethod 的构造函数中传入待连接的地址
    3. 配置要传输的参数,和消息头信息
    4. 调用第一步中创建好的实例的 execute 方法来执行第二步中创建好的 method 实例
    5. 通过response读取字符串

          6. 释放连接。无论执行方法是否成功,都必须释放连接

    代码:下面是分别发送http get、post和soap post请求:

    
    
    import java.io.FileInputStream;
    
    import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.methods.GetMethod;
    import org.apache.commons.httpclient.methods.PostMethod;
    
    public class MyHttpClient
    {
        //webservice请求地址
        private static String wsURL=
            "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";
        //创建HttpClient 的实例 
        private static HttpClient httpClient = new HttpClient();
        /**
         * 发送get请求
         */
        public static void sendGet() throws Exception{
            //创建get对象
            GetMethod get = new GetMethod(wsURL + "?mobileCode=18323455678&userID=");
            //发送get请求
            int resultCode = httpClient.executeMethod(get);
            System.out.println("Get请求结果:resultCode="+resultCode +", message="+get.getResponseBodyAsString());
            //释放get请求资源
            get.releaseConnection();
        }
        
        /**
         * 发送post请求
         */
        public static void sendPost() throws Exception{
            //创建get对象
            PostMethod post = new PostMethod(wsURL);
            post.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            post.setRequestBody("mobileCode=18323455678&userID=");
            //发送get请求
            int resultCode = httpClient.executeMethod(post);
            System.out.println("Post请求结果:resultCode="+resultCode +", message="+post.getResponseBodyAsString());
            //释放get请求资源
            post.releaseConnection();
        }
        
        /**
         * soap方式的post请求
         * soap:在http的基础上,可以发送xml格式数据
         */
        public static void sendSoapPost() throws Exception {
            // 创建一个post请求,类似Post请求
            PostMethod postMethod = new PostMethod("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx");
            // 设置传送信息的格式
            postMethod.setRequestHeader("Content-Type","text/xml; charset=utf-8");
            postMethod.setRequestBody(new FileInputStream("C:/a.txt"));
            int code = httpClient.executeMethod(postMethod);
            System.out.println("SOAP请求start..." + code);
            System.out.println("消息码为:" + code);
            System.out.println("返回的消息为:" + postMethod.getResponseBodyAsString());
            postMethod.releaseConnection();
        }
        
        public static void main(String[] args) throws Exception
        {
            sendGet();
            sendPost();
            sendSoapPost();
        }
    }
    
    
    
     

    查看运行结果:

    Get请求结果:resultCode=200, message=<?xml version="1.0" encoding="utf-8"?>
    <string xmlns="http://WebXml.com.cn/">18323455678:重庆 重庆 重庆移动全球通卡</string>
    
    Post请求结果:resultCode=200, message=<?xml version="1.0" encoding="utf-8"?>
    <string xmlns="http://WebXml.com.cn/">18323455678:重庆 重庆 重庆移动全球通卡</string>
    
    SOAP请求start...
    消息码为:200
    返回的消息为:<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/"><getMobileCodeInfoResult>18323455678:重庆 重庆 重庆移动全球通卡</getMobileCodeInfoResult></getMobileCodeInfoResponse></soap:Body></soap:Envelope>
    逃避不一定躲得过,面对不一定最难过
  • 相关阅读:
    腾讯TBS加载网页无法自适应记录
    filter过滤器实现验证跳转_返回验证结果
    Oracle不连续的值,如何实现查找上一条、下一条
    springmvc.xml 中 <url-pattern></url-pattern>节点详解
    spring拦截器-过滤器的区别
    (转)spring中的拦截器(HandlerInterceptor+MethodInterceptor)
    @Value("${xxxx}")注解的配置及使用
    mybatis BindingException: Invalid bound statement (not found)
    spring声明式事务管理方式( 基于tx和aop名字空间的xml配置+@Transactional注解)
    Spring事务管理详解_基本原理_事务管理方式
  • 原文地址:https://www.cnblogs.com/yangzhenlong/p/5163675.html
Copyright © 2011-2022 走看看