• 获取服务端数据 Post


    1,urlencoder发送数据

    NameValuePair pair = new NameValuePair("cnname",appliName);

    PostMethod postMethod = new PostMethod(url) ;
    postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8") ;
    //参数设置,需要注意的就是里边不能传NULL,要传空字符串
    NameValuePair[] data = {pair};
    postMethod.setRequestBody(data);
    HttpClient httpClient = new HttpClient();
    int i = httpClient.executeMethod(postMethod);// 执行POST方法
    if(200 == i){
    String responseBodyAsString = postMethod.getResponseBodyAsString();
    JSONObject obj = (JSONObject) JSON.parse(responseBodyAsString);
    }

    2.body

    HttpClient httpclient = new HttpClient();
    PostMethod postMethod = new PostMethod(findClaimUnderwriteDataUrl);
    RequestEntity se = new StringRequestEntity(JSONObject.toJSONString(请求体), "application/json", "UTF-8");
    postMethod.setRequestEntity(se);
    httpclient.executeMethod(postMethod);
    String resp = postMethod.getResponseBodyAsString();

    3.RestTemplate

    <dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    </dependency>

    SpringBoot启动类添加:

    @Bean
    public RestTemplate restTemplate(){
    return new RestTemplate(new OKHttp3ClientHttpRequestFactory());
    }

    ResponseEntity<Map> entity = restTemplate.getForEntity(url,Map.class);
    Map map = entity.getBody();

    4.servlet

    @SuppressWarnings("serial")
    public class Customer1 extends HttpServlet {

        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.setCharacterEncoding("UTF-8");
            resp.setContentType("application/json;charset=UTF-8");

            resp.getWriter().print(
                "{"response": {"id": "xxxxxxxx"}}");
               
            resp.getWriter().close();
        }

        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
        }
    }

  • 相关阅读:
    协方差的意义
    ios7新特性实践
    微信支付大盗--黑色产业链
    UVA 297 Quadtrees(四叉树建树、合并与遍历)
    HDU 2876 Ellipse, again and again
    java中接口的定义与实现
    Oracle Linux 6.3下安装Oracle 11g R2(11.2.0.3)
    Fortran使用隐形DO循环和reshape给一维和多维数组赋初值
    Java实现 蓝桥杯VIP 算法训练 成绩的等级输出
    Java实现 蓝桥杯VIP 算法训练 成绩的等级输出
  • 原文地址:https://www.cnblogs.com/god-monk/p/8145337.html
Copyright © 2020-2023  润新知