使用HttpURLConnection方式访问接口

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("手机号码"));
        
    }
}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。