post方式发送和解析xml

来源:互联网 发布:软件测试新手入门 编辑:程序博客网 时间:2024/06/01 08:54
 public void etcauthorize(String authCode, String clientState) {        logger.info("CMCasController- etcauthorize start... ");        View view = new View(SUCCESS);        PostMethod myPost = null;        try {           StringBuffer xmlparam = new StringBuffer();           xmlparam.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")                   .append("<GetAccessTokenReqest xmlns=\"http://www.chinamobile.com.cn/schema/omp/idmp/v1_0\">")                   .append("<grantType>authorization_code</grantType>")                   .append("<clientId>" + appid + "</clientId>")                   .append("<clientSecret>" + clientSecret + "</clientSecret>")                   .append("<authCode>" + authCode + "</authCode>")                   .append("<redirectUri>" + URLEncoder.encode(PageUtil.parseUrl(redirectUri), "utf-8") + "</redirectUri>")                   .append("<display>mobile</display>")                   .append("</GetAccessTokenReqest>");           logger.info("CMCasController- etcauthorize 跳转到中移動授权页面 請求 ", xmlparam.toString());           HttpClient httpclient = new HttpClient();           myPost = new PostMethod("https://open.mmarket.com/omee-aus/services/GetAccessToken");           myPost.setRequestHeader("Content-Type","text/xml");             myPost.setRequestHeader("charset","utf-8");             myPost.setRequestBody(xmlparam.toString());// 这里添加字符串           int resultcode = httpclient.executeMethod(myPost);           if (resultcode == 200) {               String res = myPost.getResponseBodyAsString();               logger.info("CMCasController- etcauthorize 跳转到中移動授权页面响应", res);               if (res != null) {                   //创建新的字符串                   StringReader read = new StringReader(res);                   //创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入                   InputSource source = new InputSource(read);                   //Dom方式解析xml                   DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();                                DocumentBuilder db = dbf.newDocumentBuilder();                          Document d = db.parse(source);                   String accessToken = d.getElementsByTagName("accessToken").item(0).getFirstChild().getNodeValue();                    String uniqueId = d.getElementsByTagName("uniqueId").item(0).getFirstChild().getNodeValue();                    logger.info("CMCasController-etcauthorize  获取到用户授权,accessToken:{}, uniqueId:{}" ,accessToken,uniqueId );                                  view.bind("show",getUserInfo(accessToken, uniqueId));                 }                   }                } catch (Exception e) {           logger.error("CMCasController- etcauthorize 跳转到中移動授权页面 出现异常 ", e);       } finally {           // 释放连接           myPost.releaseConnection();       }        this.render(view);       }

原创粉丝点击