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;
}
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);
}
}
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);
}
}
- httpclient返回string 和流
- HttpClient简单实例-参数传递和返回
- HttpClient doPost doGet 请求 返回内存流
- HttpClient例子一个返回html一个返回字符流
- 监控Apache Httpclient的Http请求和返回
- HttpClient和HttpURLConnection获取服务器返回的内容
- HttpClient get和HttpClient Post请求的方式获取服务器的返回数据
- HttpClient get和HttpClient Post请求的方式获取服务器的返回数据
- HttpClient(访问网络) get方式,post方式。返回值有String,InputStream,byte[]
- HttpClient(访问网络) get方式,post方式。返回值有String,InputStream,byte[]
- HttpClient(访问网络) get方式,post方式。返回值有String,InputStream,byte[]
- HttpClient(访问网络) get方式,post方式。返回值有String,InputStream,byte[]
- 解决 httpclient 返回乱码
- httpclient返回xml
- httpclient 请求返回字符串
- Json返回String对象和Object对象
- UVA401map和string的查找返回值
- httpclient和异步httpclient
- [.Net码农]EXCEL变DataTable
- iframe可编辑 (CRM不免会运用到JS)
- ExtJS的几个小技巧
- 如何设置让IE9不弹出“只显示安全内容”的提示
- oracle官方文档对连接和会话的解释
- httpclient返回string 和流
- Ext初级学习-Windows
- javaBean
- java中的向上转型和向下转型
- Mapreduce程序中的FileInputFormat.addInputPaths和FileInputFormat.addInputPath方法
- CSS温故知新
- iOS 调用系统发短信以及打电话功能
- 连接打印机提示:找不到驱动程序,windows在网络上找不到Canon LBP2900的驱动程序,若要手动查找,请单击“确定”。否则,请单击“取消”并咨询你的网络管理员或者访问打印机制造商的网站
- 1034. Head of a Gang (30)