zoukankan      html  css  js  c++  java
  • 转载:httpClient的post请求重定向302错误解决

    public class TestLogin {
    public static void main(String args[]) {
    try {
    HttpClient client = HttpClients.createDefault();
    login(client);
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    public static void login( HttpClient client) throws Exception{
    final String APPLICATION_JSON = "application/json";
    final String CONTENT_TYPE_TEXT_JSON = "text/json";

    String url = "http://172.16.30.208:8092/svc/login";
    String js = "{"username":"13800000002","password":"123456"}";

    HttpPost httpPost = new HttpPost(url);
    httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");

    StringEntity se = new StringEntity(js);
    se.setContentType(CONTENT_TYPE_TEXT_JSON);

    httpPost.setEntity(se);

    HttpResponse response = null;

    response = client.execute(httpPost);

    //----------判断是否重定向开始
    int code = response.getStatusLine().getStatusCode();
    String newuri="";
    if (code == 302) {
    Header header = response.getFirstHeader("location"); // 跳转的目标地址是在 HTTP-HEAD 中的
    newuri = header.getValue(); // 这就是跳转后的地址,再向这个地址发出新申请,以便得到跳转后的信息是啥。
    System.out.println(newuri);
    System.out.println(code);

    httpPost = new HttpPost(newuri);
    httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");

    se = new StringEntity(js);
    se.setContentType(CONTENT_TYPE_TEXT_JSON);

    httpPost.setEntity(se);

    response = client.execute(httpPost);
    code = response.getStatusLine().getStatusCode();
    System.out.println("login"+code);
    }

    //------------重定向结束
    HttpEntity entity = null;
    entity = response.getEntity();
    String s2 = EntityUtils.toString(entity, "UTF-8");
    System.out.println(s2);

    }

    }
    ————————————————
    版权声明:本文为CSDN博主「JadeHanLiang」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/qq_38497391/article/details/103402736

  • 相关阅读:
    基于DCT系数的实时监控中运动目标检测
    智能视频分析中的光照强度突然变化的处理方法
    《Single Image Haze Removal Using Dark Channel Prior》一文中图像去雾算法的原理、实现、效果及其他。
    mysql流程控制语句
    mysql中变量
    mysql中触发器
    mysql中(存储)函数
    mysql中存储过程
    mysql中视图
    mysql中一些表选项
  • 原文地址:https://www.cnblogs.com/yaohuimo/p/13303963.html
Copyright © 2011-2022 走看看