httpclient中PostMethod和HttpPost

PostMethod
String str="";
String url=" http://10.224.229.22:8086/stl-crm-itf/v1/foss/canDebitIsBeBebt/canDebitIsBeBebt";
String requestStr="{\"requestEntity\":{\"paidMethod\":\"DT\",\"deliveryCustomerCode\":\"40001555\",\"receiveOrgCode\":\"W011302020515\",\"prePayAmount\":4568},\"type\":\"synchTradSettlement\"}";
RequestEntity entity = new StringRequestEntity(requestStr,"application/json","UTF-8");
PostMethod method = new PostMethod(url);
method.setRequestEntity(entity);
method.setRequestHeader("Content-Type","application/json;charset=UTF-8");
new HttpClient().executeMethod(method);
str=method.getResponseBodyAsString();

HttpPost

RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(5000).build();//设置请求和传输超时时间
String result="";
String targetURL = interfaceVo.getDomain()+interfaceVo.getAddress(); 
HttpClient client = new DefaultHttpClient();
try {
  HttpPost httpPost = new HttpPost(targetURL);
  httpPost.setConfig(requestConfig);
  StringEntity entity = new StringEntity(JSON.toJSONString(map),"utf-8");
  entity.setContentType("application/json");
  httpPost.setEntity(entity);
  httpPost.addHeader("Authorization", "Basic "+ (new sun.misc.BASE64Encoder()).encode((userConfig.getUserName()+":"+userConfig.getPassword()).getBytes()));
//  httpPost.addHeader("Content-type","application/json");
  HttpResponse response = client.execute(httpPost);
          if(response.getStatusLine().getStatusCode()!=HttpStatus.SC_OK){
            throw new OpenedInvoiceException("", "调用腾然接口上传"+upLoadType+"网络链接失败,错误码为:"+response.getStatusLine().getStatusCode());
          }
  HttpEntity responseEntity = response.getEntity();
  result=EntityUtils.toString(responseEntity,"UTF-8");
  logger.info("------------上传"+upLoadType+"数据start-------------------");
  // 打印响应长度
  logger.info("Response content length: "+ responseEntity.getContentLength());
               // 打印响应内容
  logger.info(result);
  logger.info("------------上传"+upLoadType+"数据end-------------------");
}catch (UnsupportedEncodingException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }catch(OpenedInvoiceException e) {
throw e; 
        }catch (IOException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }finally{
   }  
return result;

HttpPost(上传文件)
public String upLoadAttachInterfaceWebService(InterfaceVoEntity interfaceVo,AttachResource attachResource,UserConfigEntity userConfig) throws IOException  {
// 判断接口域名和地址是否为空
if(StringUtils.isBlank(interfaceVo.getDomain()) || StringUtils.isBlank(interfaceVo.getAddress())) {
throw new InterfaceConfigException("接口域名(domain)或接口地址(address)或接口方法(method)为空!");
}
String result="";
String targetURL = interfaceVo.getDomain()+interfaceVo.getAddress(); 
HttpClient httpclient = new DefaultHttpClient();
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(5000).build();//设置请求和传输超时时间
HttpResponse response=null;
try {
HttpPost httppost = new HttpPost(targetURL); 
httppost.setConfig(requestConfig);
File file=getFile(attachResource);
if(!file.exists()){
logger.info(file.getPath()+"(路径文件不存在)");
result="1";
return result;
}
            FileBody bin = new FileBody(getFile(attachResource));  
            MultipartEntityBuilder reqEntity = MultipartEntityBuilder.create();  
            reqEntity.addPart("file", bin);//file1为请求后台的File upload;属性      
            httppost.setEntity(reqEntity.build());  
            httppost.addHeader("Authorization", "Basic "+ (new sun.misc.BASE64Encoder()).encode((userConfig.getUserName()+":"+userConfig.getPassword()).getBytes()));
            response = httpclient.execute(httppost); 
            if(response.getStatusLine().getStatusCode()!=HttpStatus.SC_OK){
             throw new OpenedInvoiceException("", "调用腾然接口上传附件网络链接失败,错误码为:"+response.getStatusLine().getStatusCode());
            }
            // 获取响应对象
            HttpEntity resEntity = response.getEntity();
            if(resEntity !=null) {
             result=EntityUtils.toString(resEntity,Charset.forName("UTF-8"));
             logger.info("------------上传附件数据start-------------------");
                // 打印响应长度
             logger.info("Response content length: "+ resEntity.getContentLength());
                // 打印响应内容
             logger.info(result);
             logger.info("------------上传附件数据end-------------------");
            }
            // 销毁
            EntityUtils.consume(resEntity);
}catch(InterfaceConfigException itcep) {
throw itcep;
}catch(OpenedInvoiceException e) {
throw e;
}catch (Exception e) {
System.out.println(e.getMessage());
}finally{
   }
return result;

你可能感兴趣的:(Java基础)