HttpClient与参数json

本文介绍了一个用于预加载资源的API接口调用示例,包括HTTP客户端的配置、请求参数的生成及响应结果的处理过程。该示例使用Java实现,展示了如何通过HttpPost方式发送请求并接收返回结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.tianjian.property.util;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.UUID;


import org.apache.commons.httpclient.util.URIUtil;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;




import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class JieshunPostUtil {
private static final String PRELOAD_API_URL="http://****.com";// 第三方公共接口地址
   private static final String PRELOAD_API_USERNAME="username";
   private static final String PRELOAD_API_PASSWORD="password";
   private static final String PRELOAD_API_SPEED="200k";
   private static final String PRELOAD_API_VALIDATION_TYPE="MD5";
   private static final int PRELOAD_API_NEST_TRACK_LEVEL=0;
   /**
    * 调用API公共接口
    * @param fileUrl为文件url
    * @return
    */
   @SuppressWarnings("deprecation")
public static String doPreLoadRequest(String fileUrl) {
       HttpClient httpClient=null;
       HttpPost method=null;
       String result="";


       if(null != fileUrl && fileUrl.trim().length() > 0) {
          // fileUrl=UrlUtil.getPreLoadUrl(fileUrl);
        fileUrl=fileUrl;
           httpClient=new DefaultHttpClient();
           method=new HttpPost(PRELOAD_API_URL);
       }
       String parameters=genPreLoadParams(fileUrl);
       if(method != null && parameters != null && parameters.trim().length() > 0) {
           try {
               method.setEntity(new ByteArrayEntity(parameters.getBytes("utf-8")));// 设置编码
               HttpResponse response=httpClient.execute(method);
               int statusCode=response.getStatusLine().getStatusCode();
               if(statusCode == HttpStatus.SC_OK) {
                   result=EntityUtils.toString(response.getEntity());
               }
           } catch(IOException e) {
               e.printStackTrace();
               return result;
           } finally {
               httpClient.getConnectionManager().shutdown();
           }
       }
       return result;
   }
   /**
    * 生成公共接口url参数串
    * @param fileUrl
    * @return
    */
   public static String genPreLoadParams(String fileUrl) {
       if(null == fileUrl || fileUrl.trim().length() == 0) {
           return "";
       }
       JSONObject params=new JSONObject();
       JSONObject paramObj=new JSONObject();
       JSONArray paramArray=new JSONArray();
       params.put("username", PRELOAD_API_USERNAME);
       params.put("password", PRELOAD_API_PASSWORD);
       params.put("speed", PRELOAD_API_SPEED);
       params.put("validationType", PRELOAD_API_VALIDATION_TYPE);
       params.put("nest_track_level", PRELOAD_API_NEST_TRACK_LEVEL);
      // params.put("startTime", genPreLoadExecuteTime());


       paramObj.put("id", UUID.randomUUID());
       paramObj.put("url", fileUrl);
       //paramObj.put("md5", new Md5().getMD5ofStr(fileUrl));


       paramArray.add(paramObj);
       params.put("tasks", paramArray);
       return params.toString();
   }
   /*
    * 生成预加载处理时间
    * @return string
    */
   public static String genPreLoadExecuteTime() {
       String executeTime="";
       Calendar calendar=Calendar.getInstance();


       if(calendar.get(Calendar.HOUR_OF_DAY) >= 21 && calendar.get(Calendar.HOUR_OF_DAY) <= 22) {// 晚上21,22点按流量收费,预处理时间推到第二天凌晨0点
           calendar.add(Calendar.DATE, +1);
           executeTime=new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()).concat(" 00:00:00");
       } else {// 其它时间段则设为当前时间
           executeTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
       }
       return executeTime;
   }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值