java HttpClient Post实例
2010-03-09 16:59
447 查看
private void postMethod(String url) throws IOException
{
url = "http://www.newsmth.net/bbslogin2.php";
PostMethod postMethod = new PostMethod(url);
// 填入各个表单域的值
NameValuePair[] data = { new NameValuePair("id", "herrapfel"),new NameValuePair("passwd", "") };
// 将表单的值放入postMethod中
postMethod.setRequestBody(data);
// 执行postMethod
int statusCode = httpClient.executeMethod(postMethod);
System.out.println(" status code:" + statusCode);
// HttpClient对于要求接受后继服务的请求,象POST和PUT等不能自动处理转发
if(statusCode == HttpStatus.SC_OK)
{
StringBuffer contentBuffer = new StringBuffer();
InputStream in = postMethod.getResponseBodyAsStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in,postMethod.getResponseCharSet()));
String inputLine = null;
while((inputLine = reader.readLine()) != null)
{
contentBuffer.append(inputLine);
System.out.println("input line:"+ inputLine);
contentBuffer.append("/n");
}
in.close();
}
else if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY)
{
// 从头中取出转向的地址
Header locationHeader = postMethod.getResponseHeader("location");
String location = null;
if (locationHeader != null)
{
location = locationHeader.getValue();
System.out.println("The page was redirected to:" + location);
}
else
{
System.err.println("Location field value is null.");
}
}
}
{
url = "http://www.newsmth.net/bbslogin2.php";
PostMethod postMethod = new PostMethod(url);
// 填入各个表单域的值
NameValuePair[] data = { new NameValuePair("id", "herrapfel"),new NameValuePair("passwd", "") };
// 将表单的值放入postMethod中
postMethod.setRequestBody(data);
// 执行postMethod
int statusCode = httpClient.executeMethod(postMethod);
System.out.println(" status code:" + statusCode);
// HttpClient对于要求接受后继服务的请求,象POST和PUT等不能自动处理转发
if(statusCode == HttpStatus.SC_OK)
{
StringBuffer contentBuffer = new StringBuffer();
InputStream in = postMethod.getResponseBodyAsStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in,postMethod.getResponseCharSet()));
String inputLine = null;
while((inputLine = reader.readLine()) != null)
{
contentBuffer.append(inputLine);
System.out.println("input line:"+ inputLine);
contentBuffer.append("/n");
}
in.close();
}
else if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY)
{
// 从头中取出转向的地址
Header locationHeader = postMethod.getResponseHeader("location");
String location = null;
if (locationHeader != null)
{
location = locationHeader.getValue();
System.out.println("The page was redirected to:" + location);
}
else
{
System.err.println("Location field value is null.");
}
}
}
相关文章推荐
- java HttpClient Post实例
- Java+HttpClient实现Request Payload Post数据
- HttpClient Post 二进制/字节流/byte[]实例代码
- Java乔晓松-httpURLConnection、URL、httpClient、httpPost、httpGet之代码详解篇
- Java HttpPost请求基于apache的httpclient
- Java用HttpClient3发送Get和Post请求(增强)
- Java乔晓松-httpURLConnection、URL、httpClient、httpPost、httpGet之代码详解篇
- JAVA_http的post请求 实例
- HttpClient通过Post上传文件的实例代码
- 【HttpClient】JAVA利用HttpClient进行POST和GET请求(HTTPS)
- java httpclient 抓取网页 POST GET
- java 中HttpClient传输xml字符串实例详解
- java apache commons HttpClient发送get和post请求的学习整理
- java apache commons HttpClient发送get和post请求的学习整理
- C#通过WebClient/HttpWebRequest实现http的post/get方法与java对接
- java apache commons HttpClient发送get和post请求的学习整理
- Java模拟POST表单提交HttpClient操作
- 【Java】HttpURLConnection,HttpClient 的 Get,Post 请求
- java apache commons HttpClient发送get和post请求的学习整理(转)
- java httpclient post 文件到server