httpclient post

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了httpclient post相关的知识,希望对你有一定的参考价值。

package com.dianping.cscCaseSevice;

import net.sf.json.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
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.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.Test;
import org.apache.http.NameValuePair;
import com.dianping.csc.platform.csccase.api.dto.CscCaseDto;
import com.dianping.csc.platform.csccase.api.enums.CscCaseStatusEnum;
import com.dianping.csc.platform.csccase.api.enums.CustomerType;
import com.dianping.csc.platform.csccase.api.enums.PriorityType;
import com.dianping.csc.platform.csccase.api.dto.CscCaseTakeOutDto;
import com.dianping.csc.platform.csccase.api.enums.CompensateModeEnum;
import com.dianping.csc.platform.csccase.api.enums.EvidenceSendWayEnum;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
 * Created by zjj on 16/5/24.
 */

public class testCreateCaseForMovie {
    @Test
    public void test(){
           // 创建默认的httpClient实例.
            CloseableHttpClient httpclient = HttpClients.createDefault();
           // 创建httppost
            HttpPost method = new HttpPost("http://10.66.56.86:8080/api/case/createCaseForMovie");
           // 创建参数队
            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            Date d = new Date();
            nvps.add(new BasicNameValuePair("sfCaseId", "1"));// 红谷滩新闻资讯,channelId 77
            nvps.add(new BasicNameValuePair("customerName", "meme"));// 标题
            nvps.add(new BasicNameValuePair("bindNumber", "123"));// 资讯内容
            nvps.add(new BasicNameValuePair("callerNumber", "1234"));// 资讯源地址
            nvps.add(new BasicNameValuePair("dealGroupId", "123456"));// 来源网站名称
            nvps.add(new BasicNameValuePair("dealGroupName", "keyide"));// 采用 url,url,url 的格式进行图片的返回
            nvps.add(new BasicNameValuePair("orderType", "2"));
            nvps.add(new BasicNameValuePair("comments", "可以吧"));
            nvps.add(new BasicNameValuePair("caseContent", "又有"));
            nvps.add(new BasicNameValuePair("acceptId", "123"));
            nvps.add(new BasicNameValuePair("acceptGroupId", "234"));
            nvps.add(new BasicNameValuePair("customerRequire","2345"));
            nvps.add(new BasicNameValuePair("orderNumber","234567"));
            nvps.add(new BasicNameValuePair("dealPeriod", "20"));
            try {
                    HttpEntity entity = new UrlEncodedFormEntity(nvps,"utf-8");
                    method.setEntity(entity);
                    try {
                            CloseableHttpResponse result = httpclient.execute(method);
                            if (result != null) {
                                    String resData = EntityUtils.toString(result.getEntity());
                                    System.out.println("--------------------------------------");
                                    System.out.println("Response content: " + resData);
                                    System.out.println("--------------------------------------");
                                    method.abort();
                            }
                    }
                    catch (Exception e){}
                    finally {
                            // 关闭连接,释放资源
                            try {
                                    httpclient.close();
                            } catch (IOException e) {
                                    e.printStackTrace();
                            }
                    }
            }
            catch (Exception E){}

    }
}

 

以上是关于httpclient post的主要内容,如果未能解决你的问题,请参考以下文章

HttpClient学习--HttpClient的POST请求过程源码解读

Android下通过HttpClient执行 HTTP POST 请求 的代码

HttpClient 使用 Post 方法传输文件

HttpClient 使用 Post 方法传输文件

httpclient 中post请求重定向

Java使用HttpClient实现Post请求