将一个XML数据,做为post请求的参数,发起http请求

package com.ces.common.utils;

 

import java.io.ByteArrayInputStream;

import java.io.DataOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.UnsupportedEncodingException;

import java.net.SocketTimeoutException;

import java.net.URL;

import java.net.URLConnection;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Date;

import java.util.List;

import java.util.Map;

 

import net.sf.json.JSONObject;

 

import org.apache.commons.httpclient.HttpClient;

import org.apache.commons.httpclient.HttpException;

import org.apache.commons.httpclient.HttpStatus;

import org.apache.commons.httpclient.methods.InputStreamRequestEntity;

import org.apache.commons.httpclient.methods.PostMethod;

import org.apache.commons.httpclient.methods.RequestEntity;

import org.apache.commons.io.IOUtils;

import org.apache.commons.io.output.ByteArrayOutputStream;

import org.apache.http.HttpResponse;

import org.apache.http.client.config.RequestConfig;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.entity.StringEntity;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClientBuilder;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.util.EntityUtils;

import org.dom4j.Document;

import org.dom4j.DocumentHelper;

import org.dom4j.Element;

 

 

 

/**

 *文件地址在

 所在位置:E:\学习文档子目录压缩\工具技术2\将一个XML数据,做为post请求的参数,发起http请求

 

 

 

或 我的网盘/我的笔记/学习文档子目录压缩/工具技术2\将一个XML数据,做为post请求的参数,发起http请求

 * @author lqx

 *

 */

public class DrSignUtil {

         //url--测试的

         public static String rejectedUrl = "http://IP:8080/xiangmu/qsDaArchive?";

        

        

        

 

         /**

          *

          * @param m

          * @return

          */

         public static String qsService(String loginUserName,Map<String, String> m)throws Exception{

                  Document document = DocumentHelper.createDocument();

                  Element root = document.addElement("root");

                  //头信息

                  Element head = root.addElement("head");

                  Element username = head.addElement("username");

                  username.addText(DesUtils.Encrytor(loginUserName));

                  //信息体

                  Element body = root.addElement("body");

                  Element recode = body.addElement("recode");

                  Element fields = recode.addElement("fields");

                  for (String key : m.keySet()) {

                          Element field = fields.addElement("field");

                          field.addAttribute("name", key);

                          field.addText(m.get(key)==null?"":m.get(key));

                  }

                  DebugUtils.sysOut("调用档案签收接口xml参数:"+document.asXML());

                  String qsxmlString=document.asXML();

                  DebugUtils.sysOut("签收xml:"+qsxmlString);

                  return qsxmlString;

         }

        

         public static String getUrl(String url){

                  return url+"replayId="+new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());

         }

        

        

         //将一个XML数据,做为post请求的参数,发起http请求

         public static String fileSigned(String qsStr) {

        String url = null;

        JSONObject response = null;

        String resultState = "";

      

        try {

            CloseableHttpClient httpClient = HttpClients.createDefault();

            RequestConfig requestConfig = RequestConfig.custom()

                    .setSocketTimeout(300 * 1000)

                    .setConnectTimeout(300 * 1000)

                    .build();

            url = getUrl(rejectedUrl);

            HttpPost post = new HttpPost(url);

            post.setConfig(requestConfig);

            post.setHeader("Content-Type","application/json;charset=utf-8");

                          System.out.println(qsStr);

            StringEntity postingString = new StringEntity(qsStr,"utf-8");

            post.setEntity(postingString);

            HttpResponse res = httpClient.execute(post);

                          if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

                                   String result = EntityUtils.toString(res.getEntity());// 返回json格式:

                                   System.out.println(result);

                                   response = JSONObject.fromObject(result);

                                   resultState = response.get("resultState").toString();

                                   if(resultState.equals("1")){

                                            resultState = "1";

                                   }else if(resultState.equals("2")){

                                            System.out.println("访问错误!错误类型"+response.get("errorMsg"));

                                   }else if(resultState.equals("0")){

                                            System.out.println("接口没有没有数据!");

                                   }

                          }else{

                                   System.out.println("访问接口失败,状态码"+res.getStatusLine().getStatusCode());

                          }

        } catch (SocketTimeoutException e) {

                e.printStackTrace();

        } catch (Exception e) {

                e.printStackTrace();

        }

        return resultState;

    }

         public static void main(String[] args) throws HttpException, IOException {

                 

                  //260120180301501167

                  //call_1_U("260120180301501167","uuuu_测试");

                  //call_1_G("260120180301501167");

                  //call_2_U("260120180301501167");

                  //call_2_G("260120180301501167");

                  //call_3_U("260120180301501167");

                  //call_3_G("260120180301501167");

         }

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值