java中httpclent以post方式提交请求时,http返回码是302重定向时,不自动跳转的处理
场景:
接口A:请求地址为http://www.test.com/test
接口A的功能,会把接收到的参数url写入到header的Location属性中
在java中,使用httpclient发送请求,
在以get方式发送请求时,http的返回码是200,自动处理了重定向
在以post方式发送请求时,http的返回码是302,不自动重定向,而且处于占用连接等待状态
此时的处理:
针对返回结果,处理返回码为302的情况
HttpPost httpPost = new HttpPost(url); //请求地址 StringEntity reqEntity = new StringEntity(str);//表单 HttpResponse response = httpclient.execute(httppost); httppost.abort();//释放post请求 //处理http返回码302的情况 if (response.getStatusLine().getStatusCode() == 302) { String locationUrl=response.getLastHeader("Location").getValue(); get(locationUrl);//跳转到重定向的url }