博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用Java模拟一个Post表单提交(302和301)
阅读量:5106 次
发布时间:2019-06-13

本文共 2558 字,大约阅读时间需要 8 分钟。

public String getPostResponse(String url, Map parmMap)    {        String response = null;        PostMethod post = new PostMethod(url);        HttpClient client = new HttpClient();        Iterator it = parmMap.entrySet().iterator();        NameValuePair[] param = new NameValuePair[parmMap.size()];        int i = 0;        while (it.hasNext())        {            Entry parmEntry = (Entry) it.next();            param[i++] = new NameValuePair((String) parmEntry.getKey(), (String) parmEntry.getValue());        }        post.setRequestBody(param);        try        {            int statusCode = client.executeMethod(post);            if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY)            {                Header locationHeader = post.getResponseHeader("location");                String location = null;                if (locationHeader != null)                {                    location = locationHeader.getValue();                    response = this.getPostResponse(location, param);//用跳转后的页面重新请求。                   }            }            else if(statusCode == HttpStatus.SC_OK)            {                response= post.getResponseBodyAsString();            }        }        catch (IOException ex)        {        }        finally        {            post.releaseConnection();        }        return response;    }        public String getPostResponse(String url, NameValuePair[] param)    {        String response = null;        PostMethod post = new PostMethod(url);        HttpClient client = new HttpClient();        post.setRequestBody(param);        try        {            int statusCode = client.executeMethod(post);            if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY)            {                Header locationHeader = post.getResponseHeader("location");                String location = null;                if (locationHeader != null)                {                    location = locationHeader.getValue();                    response = this.getPostResponse(location, param);//用跳转后的页面重新请求。                   }            }            else if(statusCode == HttpStatus.SC_OK)            {                response= post.getResponseBodyAsString();            }        }        catch (IOException ex)        {        }        finally        {            post.releaseConnection();        }        return response;    }

以上是java模拟一个Post表单提交,其中包含对跳转的解决。

转载于:https://www.cnblogs.com/XL-Liang/archive/2012/05/21/2512069.html

你可能感兴趣的文章
MyBaits动态sql语句
查看>>
HDU4405(期望DP)
查看>>
拉格朗日乘子法 那些年学过的高数
查看>>
vs code 的便捷使用
查看>>
Spring MVC @ResponseBody返回中文字符串乱码问题
查看>>
用户空间与内核空间,进程上下文与中断上下文[总结]
查看>>
JS 中的跨域请求
查看>>
JAVA开发环境搭建
查看>>
mysql基础语句
查看>>
Oracle中的rownum不能使用大于>的问题
查看>>
[Data Structure & Algorithm] 有向无环图的拓扑排序及关键路径
查看>>
cassandra vs mongo (1)存储引擎
查看>>
Visual Studio基于CMake配置opencv1.0.0、opencv2.2
查看>>
遍历Map对象
查看>>
MySQL索引背后的数据结构及算法原理
查看>>
#Leetcode# 209. Minimum Size Subarray Sum
查看>>
SDN第四次作业
查看>>
DM8168 DVRRDK软件框架研究
查看>>
django迁移数据库错误
查看>>
yii 跳转页面
查看>>