java用httpClient post方式传数据,ASP.NET怎么接收数据?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java用httpClient post方式传数据,ASP.NET怎么接收数据?相关的知识,希望对你有一定的参考价值。
asp.net没用过,不过,java的httpClient post和通过IE访问是一样的。asp.net只要正常处理就可以了 参考技术A 用request获取用户请求的数据,Request.Form(element)[(index)|.Count] 参考技术B 用request获取请求数据 参考技术C 传json格式的吧,传json都可以的HttpClient通过Post方式发送Json数据
转载:http://blog.csdn.net/majian_1987/article/details/47728769
服务器用的是Springmvc,接口内容:
- @ResponseBody
- @RequestMapping(value="/order",method=RequestMethod.POST)
- public boolean order(HttpServletRequest request,@RequestBody List<Order> orders) throws Exception {
- AdmPost admPost = SessionUtil.getCurrentAdmPost(request);
- if(admPost == null){
- throw new RuntimeException("[OrderController-saveOrUpdate()] 当前登陆的用户职务信息不能为空!");
- }
- try {
- this.orderService.saveOrderList(orders,admPost);
- Loggers.log("订单管理",admPost.getId(),"导入",new Date(),"导入订单成功,订单信息--> " + GsonUtil.toString(orders, new TypeToken<List<Order>>() {}.getType()));
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- Loggers.log("订单管理",admPost.getId(),"导入",new Date(),"导入订单失败,订单信息--> " + GsonUtil.toString(orders, new TypeToken<List<Order>>() {}.getType()));
- return false;
- }
- }
通过ajax访问的时候,代码如下:
- $.ajax({
- type : "POST",
- contentType : "application/json; charset=utf-8",
- url : ctx + "order/saveOrUpdate",
- dataType : "json",
- anysc : false,
- data : {orders:[{orderId:"11",createTimeOrder:"2015-08-11"}]}, // Post 方式,data参数不能为空"",如果不传参数,也要写成"{}",否则contentType将不能附加在Request Headers中。
- success : function(data){
- if (data != undefined && $.parseJSON(data) == true){
- $.messager.show({
- title:‘提示信息‘,
- msg:‘保存成功!‘,
- timeout:5000,
- showType:‘slide‘
- });
- }else{
- $.messager.alert(‘提示信息‘,‘保存失败!‘,‘error‘);
- }
- },
- error : function(XMLHttpRequest, textStatus, errorThrown) {
- alert(errorThrown + ‘:‘ + textStatus); // 错误处理
- }
- });
通过HttpClient方式访问,代码如下:
- package com.ec.spring.test;
- import java.io.IOException;
- import java.nio.charset.Charset;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- 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.StringEntity;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.util.EntityUtils;
- import com.google.gson.JsonArray;
- import com.google.gson.JsonObject;
- public class APIHttpClient {
- // 接口地址
- private static String apiURL = "http://192.168.3.67:8080/lkgst_manager/order/order";
- private Log logger = LogFactory.getLog(this.getClass());
- private static final String pattern = "yyyy-MM-dd HH:mm:ss:SSS";
- private HttpClient httpClient = null;
- private HttpPost method = null;
- private long startTime = 0L;
- private long endTime = 0L;
- private int status = 0;
- /**
- * 接口地址
- *
- * @param url
- */
- public APIHttpClient(String url) {
- if (url != null) {
- this.apiURL = url;
- }
- if (apiURL != null) {
- httpClient = new DefaultHttpClient();
- method = new HttpPost(apiURL);
- }
- }
- /**
- * 调用 API
- *
- * @param parameters
- * @return
- */
- public String post(String parameters) {
- String body = null;
- logger.info("parameters:" + parameters);
- if (method != null & parameters != null
- && !"".equals(parameters.trim())) {
- try {
- // 建立一个NameValuePair数组,用于存储欲传送的参数
- method.addHeader("Content-type","application/json; charset=utf-8");
- method.setHeader("Accept", "application/json");
- method.setEntity(new StringEntity(parameters, Charset.forName("UTF-8")));
- startTime = System.currentTimeMillis();
- HttpResponse response = httpClient.execute(method);
- endTime = System.currentTimeMillis();
- int statusCode = response.getStatusLine().getStatusCode();
- logger.info("statusCode:" + statusCode);
- logger.info("调用API 花费时间(单位:毫秒):" + (endTime - startTime));
- if (statusCode != HttpStatus.SC_OK) {
- logger.error("Method failed:" + response.getStatusLine());
- status = 1;
- }
- // Read the response body
- body = EntityUtils.toString(response.getEntity());
- } catch (IOException e) {
- // 网络错误
- status = 3;
- } finally {
- logger.info("调用接口状态:" + status);
- }
- }
- return body;
- }
- public static void main(String[] args) {
- APIHttpClient ac = new APIHttpClient(apiURL);
- JsonArray arry = new JsonArray();
- JsonObject j = new JsonObject();
- j.addProperty("orderId", "中文");
- j.addProperty("createTimeOrder", "2015-08-11");
- arry.add(j);
- System.out.println(ac.post(arry.toString()));
- }
- /**
- * 0.成功 1.执行方法失败 2.协议错误 3.网络错误
- *
- * @return the status
- */
- public int getStatus() {
- return status;
- }
- /**
- * @param status
- * the status to set
- */
- public void setStatus(int status) {
- this.status = status;
- }
- /**
- * @return the startTime
- */
- public long getStartTime() {
- return startTime;
- }
- /**
- * @return the endTime
- */
- public long getEndTime() {
- return endTime;
- }
- }
以上是关于java用httpClient post方式传数据,ASP.NET怎么接收数据?的主要内容,如果未能解决你的问题,请参考以下文章
HttpClient和AsynchttpClient的get与post请求方式
java使用httpclient通过post方式提交表单失败求助