zoukankan      html  css  js  c++  java
  • java中webService

    webservice远程调用

    import java.io.ByteArrayOutputStream;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
     
    import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.HttpException;
    import org.apache.commons.httpclient.methods.PostMethod;
     
    public class MobileCodeService {
     
        public void httpGet(String mobile,String userID) throws Exception
        {
            //http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode=string&userID=string 
           URL url = new URL("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode="+mobile+"&userID="+userID);
           HttpURLConnection conn =(HttpURLConnection)url.openConnection();
           conn.setConnectTimeout(5000);
           conn.setRequestMethod("GET");
            
           if(conn.getResponseCode()==HttpURLConnection.HTTP_OK)  //200
           {
              InputStream is= conn.getInputStream();
               
              ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();  //
               
              byte [] buf = new byte[1024];
              int len = -1;
              while((len = is.read(buf))!=-1)
              {
                  //获取结果
                  arrayOutputStream.write(buf, 0, len);
              }
               
              System.out.println("Get方式获取的数据是:"+arrayOutputStream.toString());
              arrayOutputStream.close();
              is.close();
           }
        }
         
         
        public void httpPost(String mobile,String userID) throws HttpException, IOException
        {
            //访问路径   http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo
            //HttpClient访问
             
            HttpClient httpClient = new HttpClient();
            PostMethod pm = new PostMethod("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo");
             
            pm.setParameter("mobileCode", mobile);
            pm.setParameter("userID", userID);
             
            int code= httpClient.executeMethod(pm);
            System.out.println("状态码:"+code);
             
            //获取结果
            String result = pm.getResponseBodyAsString();
            System.out.println("获取到的数据是:"+result);
        }
         
        public void SOAP() throws Exception
        {
            HttpClient client = new HttpClient();
             
            PostMethod method = new PostMethod("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx");
             
            //设置访问方法的参数
            method.setRequestBody(new FileInputStream("C:\soap.xml"));
             
            method.setRequestHeader("Content-Type","text/xml; charset=utf-8");
             
            int code= client.executeMethod(method);
            System.out.println("状态码:"+code);
             
            //获取结果
            String result = method.getResponseBodyAsString();
            System.out.println("获取到的数据是:"+result);
        }
         
        public static void main(String[] args) throws Exception {
            MobileCodeService mcs=new MobileCodeService();
            mcs.httpGet("18524012513", "");
            //mcs.httpPost("18524012513", "");
            //mcs.SOAP();
        }
    }
    

      

  • 相关阅读:
    lnmp下如何建立svn版本库
    解决更新本地svn版本库,提示:工作副本已锁定 问题
    请不要在意
    ecshop在lbi库文件中添加广告位的方法(转载,试过了确实可以添加成功)
    Jquery AjaxUpload实现文件上传
    js提交表单错误:document.form.submit() is not a function
    kindeditor的使用方法
    phpcmsv9整合ucenter经验分享
    替换字符串sql语句
    初生牛犊之spring(二)
  • 原文地址:https://www.cnblogs.com/liushisaonian/p/7553851.html
Copyright © 2011-2022 走看看