码迷,mamicode.com
首页 > Web开发 > 详细

使用HttpURLConnection方式访问接口

时间:2015-01-15 17:32:35      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

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方式访问接口

标签:

原文地址:http://www.cnblogs.com/cestblog/p/4226524.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!