import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL; public class MobileCodeWebService { private static final String charSet = "utf-8"; /**
* 生成发送的XML
* @param mobileCode 手机号码
* @return XML String
*/
private String generateXmlString(String mobileCode){ StringBuilder sb = new StringBuilder();
sb.append("<?xml version=\"1.0\" encoding=\""+charSet+"\"?>");
sb.append("<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/\">");
sb.append("<soap:Body>");
sb.append("<getMobileCodeInfo xmlns=\"http://WebXml.com.cn/\">");
sb.append("<mobileCode>"+mobileCode+"</mobileCode>");
sb.append("<userID></userID>");
sb.append("</getMobileCodeInfo>");
sb.append("</soap:Body>");
sb.append("</soap:Envelope>"); return sb.toString();
} /**
* 请求WebService并获得返回数据
*/
public String rquestWS(String mobileCode){
//访问地址
String postUrl = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?op=getMobileCodeInfo";
String contentType = "text/xml; charset="+charSet; PrintWriter printWriter = null;
BufferedReader reader = null;
String result = "";
try { String requestXml = this.generateXmlString(mobileCode);
byte[] requestByte = requestXml.getBytes(); HttpURLConnection conn = (HttpURLConnection)new URL(postUrl).openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", contentType);
conn.setRequestProperty("Content-Length", requestByte.length+"");
conn.setRequestProperty("user-agent", "Mozilla/5.0");
conn.setReadTimeout(5000); //超时时间 conn.setUseCaches(false);
conn.setDoOutput(true);
conn.setDoInput(true); printWriter = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(), charSet));
printWriter.print(requestXml);
printWriter.flush(); reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while(reader.ready()){
result += reader.readLine();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{ try {
if(reader != null){
reader.close();
}
if(printWriter != null){
printWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
} } return result;
} public static void main(String[] args) { MobileCodeWebService service = new MobileCodeWebService();
System.out.println(service.rquestWS("手机号码")); }
}

使用HttpURLConnection方式访问接口的更多相关文章

  1. PHP的fsockopen方式访问接口慢的原因与优化方案

    在开发过程中常常遇到这样的需求,模拟浏览器访问某接口,并获取返回数据.我们比较常使用的方法是fsockopen与接口建立连接,然后发出指令,然后通过fgets接受返回值. 但是我们发现,通过PHP模拟 ...

  2. java通过HttpClient方式和HttpURLConnection方式调用WebService接口

    1.引入maven依赖: <dependency> <groupId>org.apache.httpcomponents</groupId> <artifac ...

  3. 两种访问接口的方式(get和post)

    跨机器.跨语言的远程访问形式一共有三种:scoket发送数据包.http发送请求.rmi远程连接: http发送请求方式:分为post和get两种方式 importjava.io.IOExceptio ...

  4. android post 方式 访问网络 实例

    android post 方式 访问网络 实例 因为Android4.0之后对使用网络有特殊要求,已经无法再在主线程中访问网络了,必须使用多线程访问的模式 该实例需要在android配置文件中添加 网 ...

  5. 没有活动事务 链接服务器的 OLE DB 访问接口 "SQLNCLI" 无法启动分布式事务

    在windows2003下执行分布式事务的时候出现如下情况. 一. 问题现象在执行分布式事务时,在sql server 2005下收到如下错误: 链接服务器"xxxxxxx"的 O ...

  6. Dojo Data Store——统一数据访问接口

    原文地址:http://www.infoq.com/cn/articles/wq-dojo-data-store 无论在传统的桌面应用还是在主流的互联网应用中,数据始终占据着软件应用中的核心地位.当下 ...

  7. SharePoint—用REST方式访问列表

    REST的定义与作用 在SharePoint 2010中,基本上有如下几种数据访问方式: 服务器端对象模型 LINQ to SharePoint Web Service 客户端对象模型 ADO.NET ...

  8. java servlet手机app访问接口(一)数据加密传输验证

    前面几篇关于servlet的随笔,算是拉通了 servlet的简单使用流程,接下去的文章将主要围绕手机APP访问接口这块出发续写,md5加密传输--->短信验证--->手机推送---> ...

  9. httpclient 认证方式访问http api/resutful api并获取json结果

    最近,因公司线上环境rabbitmq经常发生堆积严重的现象,于是跟运维组讨论,帮助开发个集中监控所有rabbitmq服务器运行情况的应用,需要通过java访问rabbitmq暴露的http api并接 ...

随机推荐

  1. 搭建springboot的ssm(spring + springmvc + mybatis)的maven项目

    最终项目目录结构 创建过程 1.创建开关SpringBootApplication 为了创建快速.我们使用idea自带的创建springboot来创建结构,当然创建普通的web项目也是可以的.(使用e ...

  2. python字符串和数值操作函数大全(非常全)

    字符串和数值型数字的操作大全 1.反斜杠\的使用规则:一般使用表示续行的操作,可以其他符号相结合组成其他的一些使用符号,转义字符\‘的使用会识别引号,使得字符串中的引号和外面本来的啊引号相区分. (1 ...

  3. python中metaclass的工作原理

    class TMetaclass(type): def __new__(cls, name, bases, attrs): print(cls, name, bases, attrs) return ...

  4. SSH Secure Shell 编码设置-----支持中文

    参考:https://www.cnblogs.com/hupf/p/6920323.html #vi /etc/sysconfig/i18n 将内容改为 LANG="zh_CN.GB1803 ...

  5. 新闻网大数据实时分析可视化系统项目——4、Zookeeper分布式集群部署

    ZooKeeper 是一个针对大型分布式系统的可靠协调系统:它提供的功能包括:配置维护.名字服务.分布式同步.组服务等: 它的目标就是封装好复杂易出错的关键服务,将简单易用的接口和性能高效.功能稳定的 ...

  6. 如何更改placeholder属性中文字颜色

    如何更改placeholder属性中文字颜色 placeholder这个属性是HTML5中新增的属性,该属性的作用是规定可描述输入字段预期值的简短的提示信息,该提示会在用户输入之前显示在输入字段中,会 ...

  7. 如何对接网建SMS短信通短信验证码接口

    1首先注册登录网建SMS网站  http://www.smschinese.cn/ 2.下载Java代码 3.JAVA调用 import java.io.UnsupportedEncodingExce ...

  8. wyh的dp入门刷题笔记

    0: 靠前感觉之前dp抄题解都是抄的题解,自己从没有真正理解过dp.wyh下了很大决心从头学dp,于是便有了这篇文章. 1.背包 前四讲01背包&多重背包&完全背包(混合背包) :樱花 ...

  9. c++中的全排列

    next_permutation函数 组合数学中经常用到排列,这里介绍一个计算序列全排列的函数:next_permutation(start,end),和prev_permutation(start, ...

  10. crm业务流程图(精简版)

    网址:https://www.processon.com/view/link/5e0be839e4b0aef94cbcee28#map如果链接失效,请及时反馈(在评论区评论),博主会及时更新