您的当前位置:首页正文

java使用httpclient模拟post请求和get请求示例

来源:图艺博知识网

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

public class TestHttpClient {

public static void main(String[] args) {

// TODO Auto-generated method stub

//创建get方法的实例

//使用系统提供的默认恢复策略

//getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new

DefaultHttpMethodRetryHandler());

//创建post方法实例

//

////填入各个表单域的值

//NameValuePair[] data = {new NameValuePair("user_name", "user_name"),new

NameValuePair("password","password")};

//

////将表单的值放入到post方法中

//postMethod.setRequestBody(data);

//

//postMethod.getParams().setParameter(

try{

//执行GET方法

//执行post方法

if(statusCode == HttpStatus.SC_MOVED_TEMPORARILY){

Header locationHeader = postMethod.getResponseHeader("Location");

String location = null;

if(locationHeader != null){

location = locationHeader.getValue();

}

postMethod = new PostMethod(location);

NameValuePair[] data1 = {new NameValuePair("user_name", "user_name"),new

NameValuePair("password","password")};

postMethod.setRequestBody(data1);

postMethod.getParams().setParameter(

if(statusCode1 != HttpStatus.SC_OK){

System.out.println("Method is wrong " + postMethod.getStatusLine());

}

}

if(statusCode != HttpStatus.SC_OK){

System.out.println("Method is wrong " + postMethod.getStatusLine());

}

InputStream responseBody = postMethod.getResponseBodyAsStream();

BufferedReader reader = new BufferedReader(new

InputStreamReader(responseBody,"utf-8"));

String line = reader.readLine();

while(line != null){

System.out.println(new String(line.getBytes()));

line = reader.readLine();

}

}

catch (HttpException e) {

// TODO: handle exception

e.printStackTrace();

}catch (IOException e) {

// TODO: handle exception

System.out.println("the line is wrong!");

e.printStackTrace();

}finally{

getMethod.releaseConnection();//释放链接

postMethod.releaseConnection();

}

}

//Inner class for UTF-8 support

public static class UTF8PostMethod extends PostMethod{

public UTF8PostMethod(String url){

super(url);

}

@Override

public String getRequestCharSet() {

//return super.getRequestCharSet();

return "UTF-8";

}

}

}

Top