httpclient提交json参数

 1 private void httpReqUrl(List<HongGuTan> list, String url)
 2             throws ClientProtocolException, IOException {
 3
 4         logger.info("httpclient执行新闻资讯接口开始。");
 5         JSONObject json = new JSONObject();
 6         DefaultHttpClient httpClient = new DefaultHttpClient();
 7
 8         HttpPost method = new HttpPost(url);
 9
10         // 设置代理
11         if (IS_NEED_PROXY.equals("1")) {
12             HttpHost proxy = new HttpHost("192.168.13.19", 7777);
13             httpClient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy);
14         }
15
16         if (list != null && list.size() > 0) {
17             logger.info("循环处理数据列表大小list.size={}", list != null ? list.size() : 0);
18
19             // 开始循环组装post请求参数,使用倒序进行处理
20             for (int i = list.size() - 1; i >= 0; i--) {
21                 HongGuTan bean = list.get(i);
22                 if (bean == null) {
23                     continue;
24                 }
25                 // 验证参数
26                 Object[] objs = { bean.getTitle(), bean.getContent(),
27                         bean.getSourceUrl(), bean.getSourceFrom(),
28                         bean.getImgUrls() };
29                 if (!validateData(objs)) {
30                     logger.info("参数验证有误。");
31                     continue;
32                 }
33                 // 接收参数json列表
34                 JSONObject jsonParam = new JSONObject();
35                 jsonParam.put("chnl_id", "11");// 红谷滩新闻资讯,channelId 77
36                 jsonParam.put("title", bean.getTitle());// 标题
37                 jsonParam.put("content", bean.getContent());// 资讯内容
38                 jsonParam.put("source_url", bean.getSourceUrl());// 资讯源地址
39                 jsonParam.put("source_name", bean.getSourceFrom());// 来源网站名称
40                 jsonParam.put("img_urls", bean.getImgUrls());// 采用 url,url,url 的格式进行图片的返回
41
42                 StringEntity entity = new StringEntity(jsonParam.toString(),"utf-8");//解决中文乱码问题
43                 entity.setContentEncoding("UTF-8");
44                 entity.setContentType("application/json");
45                 method.setEntity(entity);
46
47                 //这边使用适用正常的表单提交
48
49 //               List<BasicNameValuePair> pairList = new ArrayList<BasicNameValuePair>();
50                 //pairList.add(new BasicNameValuePair("chnl_id", "11"));
51                 //pairList.add(new BasicNameValuePair("title", bean.getTitle()));// 标题
52                 //pairList.add(new BasicNameValuePair("content", bean.getContent()));// 资讯内容
53                 //pairList.add(new BasicNameValuePair("source_url", bean.getSourceUrl()));// 资讯源地址
54                 //pairList.add(new BasicNameValuePair("source_name", bean.getSourceFrom()));// 来源网站名称
55                 //pairList.add(new BasicNameValuePair("img_urls", bean.getImgUrls()));// 采用 url,url,url 的格式进行图片的返回
56                 //method.setEntity(new UrlEncodedFormEntity(pairList, "utf-8"));
57
58
59                 HttpResponse result = httpClient.execute(method);
60
61                 // 请求结束,返回结果
62                 String resData = EntityUtils.toString(result.getEntity());
63                 JSONObject resJson = json.parseObject(resData);
64                 String code = resJson.get("result_code").toString(); // 对方接口请求返回结果:0成功  1失败
65                 logger.info("请求返回结果集{‘code‘:" + code + ",‘desc‘:‘" + resJson.get("result_desc").toString() + "‘}");
66
67                 if (!StringUtils.isBlank(code) && code.trim().equals("0")) {// 成功
68                     logger.info("业务处理成功!");
69                 } else {
70                     logger.error("业务处理异常");
71                     Constants.dateMap.put("lastMaxId", bean.getId());
72                     break;
73                 }
74             }
75         }
76     }  
时间: 2024-07-30 13:48:56

httpclient提交json参数的相关文章

httpclient发送无参数的post数据

两个问题: 1.httpclient如何发送一个没有任何参数的post数据呢? 2.Web工程如何去接收一个无参数的post呢? 起因: 今天(2014.11.10)在开发中碰到了一个问题,接口提供方提供的接口是要求使用post方式发送数据的,心想这不超简单的一个东西吗?直接post过去不就是了,但是,提供的接口是没有任何参数的,不是类似这种http://api.dutycode.com/data/parm=xxx这种接口,而是http://api.dutycode.com/data.这个地址直

通过Ajax进行Post提交Json数据的方法

下面pk10平台出租就为大家分享一篇qq:185 198 884通过Ajax进行Post提交Json数据的方法,具有很好的参考价值,希望对大家有所帮助.一起跟随ws平台出租过来看看吧js代码:1 $.ajax({2 type : "POST", 3 url: js_path + "/maintainAdd/add", 4 data : JSON.stringify(madd_data.editMaintain), 5 contentType : "appl

JSON参数解析工具类

/// <summary> /// 解析JSON参数 /// </summary> public class JSONParser { JObject jObj = null; public JSONParser(string param) { try { jObj = JObject.Parse(param); } catch (Exception ex) { LoggerHelper.Info("JSONParser Init Error:param = {0},{1

C# 用POST提交json数据

public void GetResponse(string url, string json) { Encoding encoding = Encoding.UTF8; byte[] data = encoding.GetBytes(json); //此处为为http请求url var uri = new Uri(url); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); //用此方法可以添加标准或非标准http

phpStudy1——PHP文件获取html提交的参数

示例代码: submit.html 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title></title> 6 </head> 7 <body> 8 <form id="form1" name="form1" method="post" ac

SpringMVC控制器接收不了PUT提交的参数的解决方案

摘要: SpringMVC控制器接收不了PUT提交的参数的解决方案 这次改造了下框架,把控制器的API全部REST化,不做不知道,SpringMVC的REST有各种坑让你去跳,顺利绕过它们花了我不少时间,这次来提下SpringMVC的PUT提交参数为null的情况. 照常先贴出我的控制器代码,没什么特别的,就是打印出接受到的前台参数值: @RequestMapping(value = "/{id}", method = RequestMethod.PUT) @ResponseBody

使用Javascript获得网页中通过GET方法提交的参数

下面我将写出一个函数,用来获取GET方法提交的参数 function getParameter(parameterName) { var string = window.location.search; var indexStart = string.indexOf(parameterName+"="); if(indexStart==-1) return false; var result = string.slice(indexStart+parameterName.length+

c# HttpWebRequest 模拟HTTP post 传递JSON参数

//HTTP post   JSON 参数        private string HttpPost(string Url, Object ticket)        {            DataContractJsonSerializer serializer = new DataContractJsonSerializer(ticket.GetType());            MemoryStream stream = new MemoryStream();        

HttpClient post json

public static JSONObject post(String url,JSONObject json){ HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(url); JSONObject response = null; try { StringEntity s = new StringEntity(json.toString()); s.setContentEncoding("UTF