美文网首页
Http Post方式带参数格式

Http Post方式带参数格式

作者: a9b854aded01 | 来源:发表于2017-12-11 13:54 被阅读0次

    服务是C# WCF服务 请求是java或安卓端 以POST带参方式请求服务数据

    服务端

    接口:

     //安卓基础信息缓存接口
            //获取分库信息缓存
            [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
            string GetReservoirCacheAndroid(AndroidData data);
    

    Service:

     //安卓基础信息缓存
            //获取库存信息
            public string GetReservoirCacheAndroid(AndroidData data)
            {
                  return JsonConvert.SerializeObject(MyBaseBLL.GetReservoirCacheAndroid(data.Dep_id ));
                //return Json(MyBaseBLL.GetReservoirCacheAndroid(dep_id));
            }
    

    JAVA:

    @Test
        public void test4()throws IOException, DocumentException
        {
            String url = "http://127.0.0.1:8080/BaseManage/GetReservoirCacheAndroid";
            ParameterHashMap params = new ParameterHashMap();
            params.put("dep_id",1);
            String result = ServiceHelper.post(url,params);
            ResponseMessage responseMessage = getResponseMessage(result);
           // String json = JSONUtils.writeValueAsString(responseMessage.getContent()) ;
            String json = responseMessage.getContent().toString();
            System.out.println(result);
    
    
        }
    

    ServiceHelper

     /**
         * post方式调用http发布的C#服务
         * @param url 地址 http://192.168.1.106:8080/HelloService/GetData
         * @param nameValuePairs  参数,json格式的键值对。比如:{username:222,password:123}
         * @return json格式字符串
         * @throws IOException
         * @throws DocumentException
         */
        public static String post(String url,Map<String,String> nameValuePairs) throws IOException, DocumentException {
            return doHttpRequest(RequestType.post,url,nameValuePairs);
        }
    
     public static String doHttpRequest(RequestType requestType, String url,Map<String,String> nameValuePairs) throws IOException {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpEntity httpResponseEntity = null;
            String result = "";
            HttpUriRequest httpUriRequest = null;
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(30000).setConnectionRequestTimeout(30000).build();
    
            switch (requestType) {
                case get:
                    HttpGet httpGet = new HttpGet(url);
                    httpGet.setConfig(requestConfig);
                    httpUriRequest=httpGet;
                    break;
                case post:
                    HttpPost httpPost = new HttpPost(url);
    //                httpPost.setEntity(new StringEntity(content,"utf-8"));
                    httpPost.setEntity(new StringEntity(JSONUtils.writeValueAsString(nameValuePairs),"utf-8"));//Http Body部分
                    httpPost.addHeader(HTTP.CONTENT_ENCODING,"utf-8");//Http 请求头部分
                    httpPost.addHeader(HTTP.CONTENT_TYPE, "text/json");//Http 请求头部分
                    httpPost.setConfig(requestConfig);
                    httpUriRequest=httpPost;
            }
    
            CloseableHttpResponse httpResponse =httpClient.execute(httpUriRequest);
    
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                httpResponseEntity = httpResponse.getEntity();//获取返回的实例
                result = EntityUtils.toString(httpResponseEntity,"utf-8");
            }
    
            httpResponse.close();
            httpClient.close();
            return result;
        }
    

    相关文章

      网友评论

          本文标题:Http Post方式带参数格式

          本文链接:https://www.haomeiwen.com/subject/bmeiixtx.html