String="www.baidu.com" //代理百度
URL url=new URL(connUrl); //创建url对象
HttpURLConnection conn=(HttpURLConnection)url.openConnection(); //创建连接对象
onn.setRequestProperty("Content-Type","text/html; charset=UTF-8"); //设置传递的编码方式
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
conn.setRequestMethod("POST");//post提交方式
conn.setDoInput(true);
conn.setDoOutput(true);
reponse.getWriter();//以字符流输出,代理中有可能出现编码问题
reponse.getOutputStream();以字节流输出
---------------------------------------------------------------------------------------------下面为代码---------------------------------------------------------------------------------------------
//设置post和get方法
private static final String SERVER_POST="POST";
private static final String SERVLET_GET = "GET";
//post访问方法
public static HttpURLConnection doPost(HttpServletRequest request, HttpServletResponse response,String connUrl) throws IOException{
Enumeration<String> e= request.getParameterNames();
StringBuffer param=new StringBuffer();
//获取页面表单信息
while(e.hasMoreElements())
{
String name=(String)e.nextElement();
String namepar[]=request.getParameterValues(name);
for(int i=0;i<namepar.length;i++)
{
param.append(name).append("=").append(namepar[i]).append("&");
}
}
if(param.length()!=0){
param.deleteCharAt(param.length()-1);//去除最后一个&符号
}
URL url=new URL(connUrl);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.setRequestProperty("Content-Type","text/html; charset=UTF-8");
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
conn.setRequestMethod(SERVER_POST);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.connect();
//往代理服务端传递信息
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
out.write(param.toString().getBytes("utf-8"));
out.flush();
out.close();
return conn;
}
//get访问方法
public static HttpURLConnection doGet(HttpServletRequest request, HttpServletResponse response,String connUrl) throws IOException{
Enumeration<String> e= request.getParameterNames();
StringBuffer param=new StringBuffer();
while(e.hasMoreElements())
{
String name=(String)e.nextElement();
String namepar[]=request.getParameterValues(name);
for(int i=0;i<namepar.length;i++)
{
param.append(name).append("=").append(namepar[i]).append("&");
}
}
if(param.length()!=0){
param.deleteCharAt(param.length()-1);
}
URL url=new URL(connUrl+"?"+param.toString());
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.setRequestMethod(SERVLET_GET);
conn.setRequestProperty("Content-Type","text/html; charset=UTF-8");
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
conn.connect();
return conn;
}
//执行方法,传入request和response,以及访问的Url
public static byte[] exec(HttpServletRequest request, HttpServletResponse response,String connUrl) throws Exception{
HttpURLConnection conn=null;
if(request.getMethod().equalsIgnoreCase(SERVER_POST)){
conn=doPost(request, response,connUrl);
}
else{
conn=doGet(request, response,connUrl);
}
byte[] b= readInputStream(conn.getInputStream());
response.getOutputStream().write(b);
response.getOutputStream().flush();
response.getOutputStream().close();
conn.disconnect();
return b;
}
//读取输入流
public static byte[] readInputStream(InputStream inStream) throws Exception{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while( (len = inStream.read(buffer)) !=-1 ){
outStream.write(buffer, 0, len);
}
byte[] data = outStream.toByteArray();//形成网页的流
outStream.close();
inStream.close();
return data;
}
//例子
public static void main(String[] args) throws Exception {
HttpServletRequest request = null;
HttpServletResponse response = null;
String connUrl="www.baidu.com";
exec(request,response,connUrl);
}
原版百度搜索,和代理版百度搜索
由于传入参数有编码格式的要求,此方法为get的方法,所以只能用英文,如果想用中文索索,请使用post的提交方式,
最近在做java项目,做此笔记,造福程旭媛----------------------------------------------------爱神酷星(萧星)