package com.gaowj.service.client.utils;

import java.io.IOException;

public class Native2AsciiUtils{
	
	private static String PREFIX = "\\u";
	
	public static String native2Ascii(String str) {  
		char[] chars = str.toCharArray();  
		StringBuilder sb = new StringBuilder();  
		for (int i = 0; i < chars.length; i++) {  
		  sb.append(char2Ascii(chars[i]));  
		}  
		return sb.toString();  
	}
	
	private static String char2Ascii(char c) {  
		if (c > 255) {  
		   StringBuilder sb = new StringBuilder();  
		   sb.append(PREFIX);  
		   int code = (c >> 8);  
		   String tmp = Integer.toHexString(code);  
		   if (tmp.length() == 1) {  
		      sb.append("0");  
		   }  
		  sb.append(tmp);  
		  code = (c & 0xFF);  
		  tmp = Integer.toHexString(code);  
		  if (tmp.length() == 1) {  
		    sb.append("0");  
		  }  
		  sb.append(tmp);  
		  return sb.toString();  
		} else {  
		   return Character.toString(c);  
		}  
	} 
		
	public static String ascii2Native(String str) {  
		StringBuilder sb = new StringBuilder();  
		int begin = 0;  
		int index = str.indexOf(PREFIX);  
		while (index != -1) {  
		  sb.append(str.substring(begin, index));  
		  sb.append(ascii2Char(str.substring(index, index + 6)));  
		  begin = index + 6;  
		  index = str.indexOf(PREFIX, begin);  
		}  
		sb.append(str.substring(begin));  
		return sb.toString();  
	} 


	private static char ascii2Char(String str) {  
		if (str.length() != 6) {  
		  throw new IllegalArgumentException(  
		  "Ascii string of a native character must be 6 character.");  
		}  
		if (!PREFIX.equals(str.substring(0, 2))) {  
		  throw new IllegalArgumentException(  
		  "Ascii string of a native character must start with \"\\u\".");  
		}  
		String tmp = str.substring(2, 4);  
		int code = Integer.parseInt(tmp, 16) << 8;  
		tmp = str.substring(4, 6);  
		code += Integer.parseInt(tmp, 16);  
		return (char) code;  
	} 
	
	public static void main(String args[]){
		System.out.println(Native2AsciiUtils.native2Ascii("http://192.1345.ere/asdsadas����"));
		System.out.println(Native2AsciiUtils.ascii2Native("http://192.1345.ere/asdsadas\u5b81\u6ce2"));
//		try {
//			sendMsgNew();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} 
	}
	
	public static void sendMsgNew() throws java.io.IOException {

			String ApplicationID = "8029001";
			String Password = "XqaPhsvQ2YLW";
			String extendCode = "2";
			String phoneNo = "13566541205";
			String message = "���������������Ա������֪ͨ����û�е�С���ѻ����迴!";
			String SOAPUrl = "http://nb034.openmas.net:9080/OpenMasService";
			String SOAPAction = "http://openmas.chinamobile.com/sms/ISms/SendMessage3";
						String soapxml = "";
						soapxml += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
						soapxml += "<soapenv:Envelope ";
						soapxml += "xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">";
						soapxml += "<soapenv:Body>";
						soapxml += "<ns4:SendMessage3 xmlns:ns4=\"http://openmas.chinamobile.com/sms\">";
						// soapxml +=
						// "<SendMessage3 xmlns=\"http://openmas.chinamobile.com/sms\">";
						soapxml += "<ns4:destinationAddresses>";
						// soapxml += "<destinationAddresses>";
						soapxml += "<ns1:string xmlns:ns1=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">";
						soapxml += phoneNo;
						soapxml += "</ns1:string>";
						// soapxml +=
						// "<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">"+phoneNo+"</string>";
						soapxml += "</ns4:destinationAddresses>";
						// soapxml += "</destinationAddresses>";
						soapxml += "<ns4:message>";
						soapxml += message;
						soapxml += "</ns4:message>";
						// soapxml += "<message>"+message+"</message>";
						soapxml += "<ns4:extendCode>" + extendCode + "</ns4:extendCode>";
						// soapxml += "<extendCode>"+extendCode+"</extendCode>";
						soapxml += "<ns4:applicationId>" + ApplicationID
								+ "</ns4:applicationId>";
						// soapxml += "<applicationId>"+ApplicationID+"</applicationId>";
						soapxml += "<ns4:password>" + Password + "</ns4:password>";
						// soapxml += "<password>"+Password+"</password>";
						soapxml += "</ns4:SendMessage3>";
						// soapxml += "</SendMessage3>";
						soapxml += "</soapenv:Body>";
						soapxml += "</soapenv:Envelope>";

						java.net.URL url = new java.net.URL(SOAPUrl);
						java.net.URLConnection connection = url.openConnection();

						connection.setDoOutput(true);
						connection.setRequestProperty("Pragma:", "no-cache");
						connection.setRequestProperty("Cache-Control", "no-cache");
						connection.setRequestProperty("Content-Type", "text/xml");
						connection.setRequestProperty("SOAPAction", SOAPAction);

						java.io.OutputStreamWriter outs = new java.io.OutputStreamWriter(connection
								.getOutputStream(),"utf-8");
						
//						outs.write(new String(soapxml.getBytes("utf-8")));
						
						outs.write(soapxml);
						outs.flush();
						outs.close();

						java.io.BufferedReader br = new java.io.BufferedReader(new java.io.InputStreamReader(connection
								.getInputStream(), "utf-8"));

						StringBuilder sBuilder = new StringBuilder();
						String line = "";
						for (line = br.readLine(); line != null; line = br.readLine()) {
							// sBuilder.append(line);
						}
						System.out.println("�ѷ���");
						System.out.println(sBuilder.toString());
	}


}
