新建一个类:HttpTools.java,粘贴一下代码
import java.io.*;import java.net.HttpURLConnection;import java.net.InetSocketAddress;import java.net.Proxy;import java.net.URL;import java.net.URLConnection;import com.bony.demo.Log;public class HttpTools {/** * 向指定URL发送GET方法的请求 * @author bony * @param url 发送请求的URL * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。 * @return URL 所代表远程资源的响应结果 */public static String UA=null,result=null,httpselty=null,message=null;public static int code=0;public BufferedReader buffered_reader = null;public InputStream stream= null;InputStreamReader stream_reader= null;// HttpURLConnection httpUrlConnection= null;public InputStream sendGet(String url, String param) { try { String UrlString; if(param!=""&¶m!=null)UrlString = url + "?" + param; else UrlString=url; System.out.println(UrlString); URL toURL = new URL(UrlString); Proxy proxy = new Proxy(java.net.Proxy.Type.HTTP,new InetSocketAddress("127.0.0.1", 8888)); URLConnection connection = toURL.openConnection(proxy);// 打开和URL之间的连接// URLConnection connection = toURL.openConnection();// 打开和URL之间的连接 HttpURLConnection httpUrlConnection = (HttpURLConnection) connection; httpUrlConnection.setConnectTimeout(1500);//设置链连接超时 httpUrlConnection.setReadTimeout(1500);//设置读超时 httpUrlConnection.setRequestProperty("X-KKBROWSER-UA",UA); httpUrlConnection.setRequestProperty("Content-Type", "application/x-protobuf"); httpUrlConnection.setRequestProperty("Accept", "application/x-protobuf"); httpUrlConnection.connect();//建立连接 code=httpUrlConnection.getResponseCode();//得到响应代码 message = httpUrlConnection.getResponseMessage();//回应信息 stream=httpUrlConnection.getInputStream();//创建输入流对象 } catch (Exception e) { Log.run.error("sendGet is Exception:"+e.getMessage()); System.err.println("sendGet is Exception:"+e.getMessage()); } return stream;//返回请求流}/** * 向指定 URL 发送POST方法的请求 * @author bony * @param url 发送请求的 URL * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。 * @return 所代表远程资源的响应结果 */public InputStream sendPost(String url, String param) { PrintWriter print_writer=null; try { URL toURL = new URL(url); Proxy proxy = new Proxy(java.net.Proxy.Type.HTTP,new InetSocketAddress("127.0.0.1", 8888)); URLConnection connection = toURL.openConnection(proxy);// 打开和URL之间的连接// URLConnection connection = toURL.openConnection();// 打开和URL之间的连接 HttpURLConnection httpUrlConnection = (HttpURLConnection) connection;// 发送POST请求必须设置如下两行 httpUrlConnection.setRequestProperty("X-KKBROWSER-UA",UA); httpUrlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); httpUrlConnection.setDoOutput(true); //设置输出 httpUrlConnection.setDoInput(true); //设置输入 httpUrlConnection.setConnectTimeout(1500); //设置链连接超时 httpUrlConnection.setRequestMethod("POST"); httpUrlConnection.setReadTimeout(1500); //设置读超时 httpUrlConnection.connect(); //建立连接 OutputStream output_stream = httpUrlConnection.getOutputStream();//得到输出流 print_writer=new PrintWriter(output_stream); //创建输出流对象 print_writer.write(param); print_writer.flush(); // flush输出流的缓冲 code=httpUrlConnection.getResponseCode(); //得到响应代码 message = httpUrlConnection.getResponseMessage(); //回应信息 httpselty=httpUrlConnection.getRequestMethod(); //请求方式获取 stream=httpUrlConnection.getInputStream(); //创建输入流对象 } catch (Exception e) { Log.run.error("sendPost is Exception:"+e.getMessage()); System.err.println("sendPost is Exception:"+e.getMessage());// e.printStackTrace(); } return stream; //返回请求流} /** * 抓取InputStream的字节流并返回 * @param inputStream * @return byte[] */public byte[] getResponseBody(InputStream inputStream) { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buf = new byte[2048]; InputStream is = inputStream; for (int i = 0; i != -1; i = is.read(buf)) baos.write(buf, 0, i); return baos.toByteArray(); } catch (Exception e) { return null; }}/** * http请求返回流的读取 * @return */public String getReadline(){ try { stream_reader=new InputStreamReader(stream); buffered_reader=new BufferedReader(stream_reader); //创建缓冲读写器对象 result=buffered_reader.readLine(); //读取值(页面响应内容) } catch (Exception e) { Log.run.error("get result is Exception:"+e.getMessage()); System.err.println("get result is Exception:"+e.getMessage()); } finally{ try {if(buffered_reader != null)buffered_reader.close();} catch (Exception e1){ Log.run.error("getInputStream close is Exception:"+e1.getMessage()); System.err.println("getInputStream close is Exception:"+e1.getMessage()); } } return result;//返回请求结果}}
若需要发送一个Get请求,并读取返回的数据,可以如下调用:
String url="http://baidu.com";String param="a=1&b=1";HttpTools http=new HttpTools();//新建一个工具对象http.sendGet(url, param);
若需要发送一个Post
String url="http://baidu.com";String param="{'a':'1','b':'2'}";HttpTools http=new HttpTools();//新建一个工具对象http.sendGet(url, param);
请求,并读取返回的数据,可以如下调用: