HTTP接口发送XML短信

 

记录下来,下次碰到还可以查查。
/**
	 * 发送XML短信
	 * 
	 * @param mobile
	 * @param content
	 * @return
	 */
	public static String sendMessage(String mobile, String content) {
		/*try {
			content = new String(content.getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}*/
		// XML短信格式
		String tpl = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<!DOCTYPE jds SYSTEM \"/home/httpd/html/dtd/jds2.dtd\">" + "<jds>"
				+ "<account acid=\"[account]\" loginid=\"[loginId]\" passwd=\"[password]\">" + "<msg_send>" + "<recipient>" + mobile + "</recipient>"
				+ "<content><![CDATA[" + content + "]]></content>" + "<language>C</language>" + "</msg_send>" + "</account>" + "</jds>";
		String rs = "";
		HttpURLConnection connection = null;
		try {
			System.out.println(tpl);
			// 把buffer链接存入新建的URL中
			URL url = new URL("[your url]");
			// 打开URL链接
			connection = (HttpURLConnection) url.openConnection();
			// 使用POST方式发送
			connection.setRequestMethod("POST");
			connection.setConnectTimeout(5 * 1000);// 设置超时的时间
			connection.setDoInput(true);
			connection.setDoOutput(true);// 如果通过post提交数据,必须设置允许对外输出数据
			connection.setRequestProperty("Content-Type", "text/xml;");
			connection.setRequestProperty("Content-Type", "UTF-8");
			connection.setRequestProperty("Accept-Charset", "UTF-8");
			connection.connect();
			<span style="color:#ff0000;">DataOutputStream out = new DataOutputStream(connection.getOutputStream());
			out.write(tpl.getBytes("UTF-8")); // 写入请求的字符串</span>
			out.flush();
			out.close();

			// 请求返回的状态
			if (connection.getResponseCode() == 200) {
				// 请求返回的数据
				InputStream in = connection.getInputStream();
				String a = null;
				try {
					byte[] data1 = new byte[in.available()];
					in.read(data1);
					// 转成字符串
					a = new String(data1, "UTF-8");
					rs = parseXML(a);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
			rs = String.valueOf(PublicMethod.SMS_EXCEPTION_THROWN);
		} finally {
			if (connection != null) {
				connection.disconnect();
			}
		}
		return rs;
	}

	/**
	 * 解析XML字符串,直接转成JSON
	 * 
	 * @param s
	 * @param tagName
	 * @return
	 */
	public static String parseXML(String s) {
		System.out.println(s);
		// 解析对方发来的xml数据,获得EventID节点的值
		s = s.replace("<!DOCTYPE jds SYSTEM \"jds2.dtd\">", "");
		XMLSerializer xmlSerializer = new XMLSerializer();
		try {
			JSONObject json = (JSONObject) xmlSerializer.read(s);
			if (json.get("msg_send_ret") != null) {
				JSONObject json2 = (JSONObject) json.get("msg_send_ret");
				if (json2.get("msg") != null) {
					JSONObject json3 = (JSONObject) json2.get("msg");
					if (json3.get("jobid") != null) {
						String jobid = json3.get("jobid").toString();
						if (PublicMethod.isNumeric(jobid)) {
							s = "101";
						} else {
							s = "0";
						}
					} else {
						s = "0";
					}
				} else {
					s = "0";
				}

			} else if (json.get("jerr") != null) {
				String jerr = json.get("jerr").toString();
				s = jerr;
			}
		} catch (Exception e) {
			e.printStackTrace();
			s = String.valueOf(PublicMethod.SMS_EXCEPTION_THROWN);
		}
		System.out.println(s);
		return s;
	}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值