zoukankan      html  css  js  c++  java
  • httpclient模拟服务器请求

    // 创建默认的httpClient实例.
    CloseableHttpClient httpclient = HttpClients.createDefault();
    // 创建httppost
    HttpPost httppost = //new HttpPost("http://crmetl.yishenghuo.com:8142/outserver/startUpByHand.htm");
    new HttpPost("http://localhost:8087/outserver/startUpByHand.htm");
    // 创建参数队列
    List<NameValuePair> formparams = new ArrayList<NameValuePair>();
    formparams.add(new BasicNameValuePair("userName", "admin"));
    formparams.add(new BasicNameValuePair("passWord", "123456"));
    UrlEncodedFormEntity uefEntity;
    try {
    uefEntity = new UrlEncodedFormEntity((List<? extends org.apache.http.NameValuePair>) formparams, "UTF-8");
    httppost.setEntity(uefEntity);
    CloseableHttpResponse httpResponse = httpclient.execute(httppost);
    try {
    HttpEntity entity = httpResponse.getEntity();
    if (entity != null) {
    //0:更新成功,1:更新失败,2:更新中,3:距离上次更新不足1个小时
    JSONObject json = new JSONObject();
    if("0".equals(entity)){
    json.put("code", "0");
    json.put("message", "更新成功!");
    }else if("1".equals(entity)){
    json.put("code", "1");
    json.put("message", entity+"更新失败!");
    }else if("2".equals(entity)){
    json.put("code", "2");
    json.put("message", entity+"更新中!");
    }else if("3".equals(entity)){
    json.put("code", "3");
    json.put("message", entity+"距离上次更新不足1个小时,请稍候再更新。");
    }
    json.put("message", "未知错误。。。");
    response.getWriter().print(json.toJSONString());
    logger.info("更新结果:"+EntityUtils.toString(entity, "UTF-8"));

    }
    } finally {
    httpResponse.close();
    }
    } catch (ClientProtocolException e) {
    e.printStackTrace();
    } catch (UnsupportedEncodingException e1) {
    e1.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    // 关闭连接,释放资源
    try {
    httpclient.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

  • 相关阅读:
    Swift
    iOS 拨打电话
    UI控件问题和XCode编译警告和错误解决方法集锦 (持续更新ing)
    让UIWebView弹出键盘上的按钮显示中文 (附效果图)
    启动app时全屏显示Default.png (附效果图)
    三种数据存储 (plist,NSUserDefaults存储,NSKeyedArchiver存模型方法)
    生成圆形头像 (附效果图)
    循环按钮,并且选中 (附效果图)
    leetcode 字符串分割对称
    leetcode merge-sorted-array/
  • 原文地址:https://www.cnblogs.com/xuehen/p/4898431.html
Copyright © 2011-2022 走看看