httpclient返回string 和流

来源:互联网 发布:盟军敢死队2 for mac 编辑:程序博客网 时间:2024/06/08 19:01
package com.bypay.action;


import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;


import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import org.apache.http.HttpEntity;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.struts2.ServletActionContext;


import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.annotations.BeforeResult;


public class ReqAction extends ActionSupport {
private static final long serialVersionUID = 1L;

private HttpSession session;
private static HttpClient httpClient;
private static HttpPost httpPost;
private HttpServletResponse response;

private String content = new String();

@Override
public String execute() throws Exception {
getBtAndIndex();
return "result";
}

/**

*/
@BeforeResult
public void beforeResult() {

}

//获取bt和index
private void getBtAndIndex(){
// 生
//向后台系统发送请求
//获取响应
String url="http://192.168.1.109:8080/bServer/resAction.ac";
String encode="UTF-8";
String[] params={"name","gw","password","1234"};
try {
content=getResponseByPost(url,encode,params);
session.setAttribute("htmlStr",  content);//页面显示的内容
} catch (Exception e) {
e.printStackTrace();
}
}


// public void getCode(){
// //获取响应
// String url="http://192.168.1.109:8080/bServer/resAction!getCode.ac";
// String encode="UTF-8";
// String[] params={"name","gw","password","1234"};
// try {
// InputStream in=getResponseStreamByPost(url,encode,params);
//        OutputStream os = null;
//        try{
//            os = response.getOutputStream();
//            int bytesRead = 0;
//            byte[] buffer = new byte[8192];
//            while((bytesRead = in.read(buffer,0,8192))!=-1){
//            os.write(buffer,0,bytesRead);
//            }
//       }catch (IOException e) {
//           e.printStackTrace();
//      }
//       os.close(); 
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
//httpclient请求返回string
public String getResponseByPost(String url,String encode,String[] params)throws Exception{
httpPost = new HttpPost(url); 
List<org.apache.http.NameValuePair> formParams = new ArrayList<org.apache.http.NameValuePair>();
for(int i=0; i<params.length/2;i++){
formParams.add(new BasicNameValuePair(params[i*2], params[i*2+1]));
}
HttpEntity entityForm = new UrlEncodedFormEntity(formParams, encode);
httpPost.setEntity(entityForm);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
content = httpClient.execute(httpPost,responseHandler);
return content;
}

//httpclient请求返回流

public InputStream getResponseStreamByPost(String url,String encode,String[] params)throws Exception{
httpPost = new HttpPost(url); 
List<org.apache.http.NameValuePair> formParams = new ArrayList<org.apache.http.NameValuePair>();
for(int i=0; i<params.length/2;i++){
formParams.add(new BasicNameValuePair(params[i*2], params[i*2+1]));
}
HttpEntity entityForm = new UrlEncodedFormEntity(formParams, encode);
httpPost.setEntity(entityForm);
return  httpClient.execute(httpPost).getEntity().getContent();
}

//构造方法
public ReqAction(){
httpClient = new DefaultHttpClient();
session =ServletActionContext.getRequest().getSession();
response=ServletActionContext.getResponse();
httpClient.getParams().setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT, 90000);
httpClient.getParams().setIntParameter(HttpConnectionParams.SO_TIMEOUT, 120000);
httpClient.getParams().setIntParameter(HttpConnectionParams.SOCKET_BUFFER_SIZE, 1024*4);
}
}
原创粉丝点击