首页 > 学院 > 开发设计 > 正文

用java实现新浪爬虫,代码完整剖析(仅针对当前SinaSignOn有效)

2019-11-15 00:13:31
字体:
来源:转载
供稿:网友
java实现新浪爬虫,代码完整剖析(仅针对当前SinaSignOn有效)

先来看我们的web.xml文件,如下

 1 <!DOCTYPE web-app PUBLIC 2  "-//Sun Microsystems, Inc.//DTD Web application 2.3//EN" 3  "http://java.sun.com/dtd/web-app_2_3.dtd" > 4  5 <web-app> 6   <display-name>MySinaspider</display-name> 7     <listener> 8         <listener-class>main.java.sina.spider.StartSpiderLisenter</listener-class> 9       </listener>10 </web-app>

这样的配置当启动tomcat的时候,就会运行爬虫,然后再看我们的StartSpiderLisenter类,如下

 1 package main.java.sina.spider; 2  3 import javax.servlet.ServletContextEvent; 4 import javax.servlet.ServletContextListener; 5 import main.java.sina.bean.info.LoginInfo; 6 import main.java.sina.utils.Constant; 7  8 public class StartSpiderLisenter implements ServletContextListener{ 9 10     public void contextDestroyed(ServletContextEvent arg0) {11         12     }13 14     public void contextInitialized(ServletContextEvent arg0) {15         Constant.personalHomePage = "http://weibo.com/zhaoyao2012/home"; //填写你自己的新浪微博个人主页16         LoginInfo.username = "***"; //填写你的新浪微博用户名18         LoginInfo.passWord = "***"; //填写你的新浪微博密码19         Constant.enablePRoxy = false; //是否使用代理20         Spider.start();21     }22 23 }

很明显我们看到StartSpiderLisenter 类是继承自ServletContextListener这个接口,一定要实现它的两个方法,contextInitialized和contextDestroyed.它们分别在初始化和销毁的时候被容器调用。我们看到在contextInitialized初始化上下文的方法中调用了Spider.start()方法。那么我们来看看Spider这个类,如下:

  1 package main.java.sina.spider;  2   3 import java.io.IOException;  4 import java.util.regex.Matcher;  5 import java.util.regex.Pattern;  7 import org.quartz.JobBuilder;  8 import org.quartz.JobDetail;  9 import org.quartz.Scheduler; 10 import org.quartz.SchedulerException; 11 import org.quartz.SchedulerFactory; 12 import org.quartz.SimpleScheduleBuilder; 13 import org.quartz.SimpleTrigger; 14 import org.quartz.TriggerBuilder; 15 import org.quartz.impl.StdSchedulerFactory; 17 import main.java.sina.bean.info.LoginInfo; 18 import main.java.sina.httpclient.LoginSina; 19 import main.java.sina.httpclient.SpiderSina; 20 import main.java.sina.job.KeywordSearchJob; 21 import main.java.sina.utils.Constant; 22 import main.java.sina.utils.HttpHelper; 23 import main.java.test.SpiderTest; 24  25 public class Spider { 26  27     public static void main(String[] args) { 28  29         Constant.personalHomePage = "****";     30         LoginInfo.username = "****"; 31         LoginInfo.password = "****"; 32         Constant.enableProxy = false; 33         Constant.hourbefore = 0;  //这个参数用于设置时差 34         start(); 35          36     } 37     public static void start() { 38          39         final SchedulerFactory factory = new StdSchedulerFactory(); 40         try { 41             Scheduler scheduler = factory.getScheduler(); 42             JobDetail jobDetail = JobBuilder.newJob(KeywordSearchJob.class) 43                     .withIdentity("keywordSearch", "weibo").build(); 44             SimpleTrigger trigger = TriggerBuilder.newTrigger() 45                     .withIdentity("keywordSearch", "weibo") 46                     .withSchedule(SimpleScheduleBuilder.repeatHourlyForever()) 47                     .build(); 48             scheduler.scheduleJob(jobDetail, trigger); 49             scheduler.start(); 50         } catch (SchedulerException e) { 51             e.printStackTrace(); 52         } 53     } 54  55     public static SpiderSina createSpider() { 56         LoginSina ls = new LoginSina(LoginInfo.username, LoginInfo.password); 57         ls.dologinSina(); 58         ls.redirect(); 59         SpiderSina spider = new SpiderSina(ls); 60  61         return spider; 62     } 63  64     public static void sendMidsofDays(SpiderSina spider,String keyword, String fromdate, 65             String todate) { 66          67         try { 68             String midsString = ""; 69             for (int i = 1; i <= 50; i++) { 70                 String htmlContent = spider 71                         .search(keyword, i, fromdate, todate); 72                 if (htmlContent.contains("noresult_support")) { 73                     break; 74                 } 75                 System.out.println(i); 76                 Pattern pattern = Pattern.compile("<div mid=/"([0-9]*)/""); 77  78                 String start = "/"pid/":/"pl_weibo_direct/""; 79                 try { 80                     htmlContent = htmlContent.substring(htmlContent 81                             .indexOf(start)); 82                 } catch (Exception e) { 83                     htmlContent = htmlContent.substring(1); 84                 } 85                 htmlContent = htmlContent.replace("///"", "/""); 86                 htmlContent = htmlContent.replace("///", "/"); 87                 Matcher matcher = pattern.matcher(htmlContent); 88                 while (matcher.find()) { 89                     System.out.println(matcher.group(1)); 90                     midsString += matcher.group(1) + ","; 91                 } 92                 if (i == 37) { 93                     try { 94                         Thread.sleep(1000 * 60 * 30); 95                     } catch (InterruptedException e) { 96                         e.printStackTrace(); 97                     } 98                 } 99             }100             System.out.println(midsString);101             HttpHelper.getLiveData(midsString, Constant.CommentUrl);102         } catch (IOException e) {103             e.printStackTrace();104         }105 106     }107 }

我们在Spider.start()方法中,看到了作业KeywordSearchJob.class,那么我们来看看这个KeywordSearchJob类的实现,如下:

 1 package main.java.sina.job; 2  3 import org.quartz.Job; 4 import org.quartz.JobExecutionContext; 5 import org.quartz.JobExecutionException; 6 import main.java.sina.httpclient.SpiderSina; 7 import main.java.sina.spider.Spider; 8 import main.java.sina.utils.Constant; 9 import main.java.sina.utils.Utils;10 11 public class KeywordSearchJob implements Job {12 13     public void execute(JobExecutionContext arg0) throws JobExecutionException {14 15         Constant.enableProxy = false; //我的爬虫中没有使用代理,故值设为false.16         String keyword = "%25E5%25AE%2581%25E6%25B3%25A2%25E5%25A4%25A7%25E5%25AD%25A6";//被编码后的关键字17         String datehour = Utils.getDateOfSpecifiedPreHour(Constant.hourbefore);//这个工具类实现了时差格式的转换18         SpiderSina spider = Spider.createSpider();19         spider.forwardToWeiboPage();20         Spider.sendMidsofDays(spider,keyword,datehour,datehour);21     }22 23 }

接下来,我们看几个工具类的实现:首先来看下Utils.java这个类,如下:它实现了日期的格式的一些转换

  1 package main.java.sina.utils;  2   3 import java.io.BufferedReader;  4 import java.io.BufferedWriter;  5 import java.io.File;  6 import java.io.FileInputStream;  7 import java.io.FileNotFoundException;  8 import java.io.FileOutputStream;  9 import java.io.FileWriter; 10 import java.io.IOException; 11 import java.io.InputStream; 12 import java.io.InputStreamReader; 13 import java.io.StringReader; 14 import java.io.UnsupportedEncodingException; 15 import java.text.ParseException; 16 import java.text.SimpleDateFormat; 17 import java.util.Calendar; 18 import java.util.Date; 19 import java.util.Properties; 20  21 import org.htmlparser.Parser; 22 import org.htmlparser.lexer.Lexer; 23 import org.htmlparser.lexer.Page; 24 import org.htmlparser.util.DefaultParserFeedback; 25 //  I/O操作类 26 public class Utils { 27      28     public static Date getDateFromString(String dtext,Date fileCreateDate) { 29         Date date=null; 30         int y,mm,se;   31         Calendar c = Calendar.getInstance();   32         c.setTime(fileCreateDate); 33         y = c.get(Calendar.YEAR); //年   34         //d = c.get(Calendar.DAY_OF_MONTH); //日 35         mm = c.get(Calendar.MINUTE); //分 36         se = c.get(Calendar.SECOND);//秒 37         if(dtext.contains("秒前")){ 38             int end=0; 39             for(int i=0;i<dtext.length();i++){ 40                 if(dtext.charAt(i)>='0' && dtext.charAt(i)<='9'){ 41                     end++; 42                 }else{ 43                     break; 44                 } 45             } 46             dtext=dtext.substring(0,end); 47             int second=Integer.parseInt(dtext); 48             c.set(Calendar.SECOND, se-second); 49             date=c.getTime(); 50         } 51         else if(dtext.contains("分钟前")){ 52             int end=0; 53             for(int i=0;i<dtext.length();i++){ 54                 if(dtext.charAt(i)>='0' && dtext.charAt(i)<='9'){ 55                     end++; 56                 }else{ 57                     break; 58                 } 59             } 60             dtext=dtext.substring(0,end); 61             int minute=Integer.parseInt(dtext); 62             c.set(Calendar.MINUTE, mm-minute); 63             date=c.getTime(); 64         }else if(dtext.contains("今天")){ 65              dtext=dtext.replace("今天 ", "").trim(); 66              String ss[]=dtext.split(":"); 67              if(ss!=null && ss.length==2){ 68                  c.set(Calendar.HOUR_OF_DAY, Integer.parseInt(ss[0])); 69                  c.set(Calendar.MINUTE, Integer.parseInt(ss[1])); 70                  date=c.getTime(); 71              } 72         }else if(dtext.contains("月")){ 73             dtext=y+"年".concat(dtext); 74             SimpleDateFormat sf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm"); 75             try { 76                 date=sf.parse(dtext); 77             } catch (ParseException e) { 78                 e.printStackTrace(); 79             } 80         }else if(dtext.contains("-")){ 81             SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm"); 82             try { 83                 date=sf.parse(dtext); 84             } catch (ParseException e) { 85                 e.printStackTrace(); 86             } 87         } 88         return date; 89     } 90     public static void writeFileFromStream(String filename,InputStream in){ 91         if(filename==null || filename.trim().length()==0) 92             return; 93         File file=new File(filename); 94         if(!file.exists()){ 95             try { 96                 file.createNewFile(); 97             } catch (IOException e) { 98                 e.printStackTrace(); 99             }100         }101         FileOutputStream fou=null;102         try {103             fou = new FileOutputStream(file);104             byte []buffer=new byte[1024*4];105             int len=-1;106             while((len=in.read(buffer))!=-1){107                 fou.write(buffer,0,len);108             }109         } catch (FileNotFoundException e) {110             e.printStackTrace();111         } catch (IOException e) {112             e.printStackTrace();113         }finally{114             if(in!=null)115                 try {116                     in.close();117                 } catch (IOException e) {118                     e.printStackTrace();119                 }120             if(fou!=null)121                 try {122                     fou.close();123                 } catch (IOException e) {124                     e.printStackTrace();125                 }126         }127     }    128     public static void writeFileFromString(String filename,String str){129         if(filename==null || filename.trim().length()==0)130             filename="tmp.txt";131         File file=new File(filename);132         if(!file.exists()){133             try {134                 file.createNewFile();135             } catch (IOException e) {136                 e.printStackTrace();137             }138         }139         BufferedWriter writer=null;140         BufferedReader reader=null;141         try {142             writer=new BufferedWriter(new FileWriter(file));143             reader=new BufferedReader(new StringReader(str));144             String tmp=null;145             StringBuffer buffer=new StringBuffer();146             while((tmp=reader.readLine())!=null)147                 buffer.append(tmp+"/n");148             writer.write(buffer.toString());149             150         } catch (IOException e) {151             e.printStackTrace();152         }finally{153             try {154                 reader.close();155                 writer.close();156             } catch (IOException e) {157                 e.printStackTrace();158             }159         }160         161     }162     163     164     165     public static String getStringFromStream(InputStream in) {166         BufferedReader reader=null;167         reader = new BufferedReader(new InputStreamReader(in));168         StringBuffer buffer=new StringBuffer();169         String str=null;170         try{171             while((str=reader.readLine())!=null){172                 buffer.append(str+"/n");173             }    174             reader.close();175         }catch(Exception ex){176             ex.printStackTrace();177         }            178         try {179             return new String(buffer.toString().getBytes(),"utf-8");180         } catch (UnsupportedEncodingException e) {181             e.printStackTrace();182             return "error:"+e.getMessage();183         }184     }185   //得到数据库的配置信息186     public static Properties getDBconfig(){187         Properties properties=new Properties();188         InputStream in = null;189         try {190             in = new FileInputStream(new File("config/dbconfig.ini"));191             properties.load(in);192         } catch (FileNotFoundException e) {193             e.printStackTrace();194         } catch (IOException e) {195             e.printStackTrace();196         }finally{197             if(in!=null)198                 try {199                     in.close();200                 } catch (IOException e) {201                     e.printStackTrace();202                 }203         }204         return properties;205     }206     207     public static Parser createParser(String inputHTML) {208         Lexer mLexer = new Lexer(new Page(inputHTML));209         Parser parser = new Parser(mLexer, new DefaultParserFeedback(210                 DefaultParserFeedback.QUIET));211         return parser;212     }213     214     public static String getDateOfSpecifiedPreHour(int hourNum){215         SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy-MM-dd-HH");216         Date date = new Date();217         System.out.println("date -" +date + " " + hourNum);218         Calendar calendar = Calendar.getInstance();219         calendar.setTime(date);220         calendar.add(Calendar.HOUR_OF_DAY, -1 * hourNum);221         System.out.println("date2 -" +sdFormat.format(calendar.getTime()));222         return sdFormat.format(calendar.getTime());223     }    224 }

再来看一下ThreadPool.java这个类,如下:这是一个线程工具类,定义了线程的一些动作

 1 package main.java.sina.utils; 2  3 import java.util.List; 4 import java.util.concurrent.ExecutorService; 5 import java.util.concurrent.Executors; 6  7 /** 9  * 线程池工具类10  */11 public class ThreadPool {12     private ExecutorService service;13     private List<Thread> threadList;14 15     public ThreadPool(int limite, List<Thread> threadList) {16         this.service = Executors.newFixedThreadPool(limite);17         this.threadList = threadList;18     }19 20     public void execute() {21         if(threadList==null ||threadList.size()==0) return ;22         for (int index = 0; index < threadList.size(); index++) {23             Thread t=threadList.get(index);24             service.execute(t);25         }26     }27     public boolean isTerminated(){28         return service.isTerminated();29     }30     31     public void shutDown() {32         service.shutdown();33     }34 }

然后再看一下Constant.java这个常量类,如下:常量类把系统总用到的一些常量写在这里,以后项目维护需要更改的时候,方便维护更改

package main.java.sina.utils;/** * @ClassName: Constant  *  */public class Constant {    public static boolean enableProxy = false;    public static String liveCommentUrl = "http://localhost:8080/social-hub-connector/loadingLiveData";    public static String CommentUrl = "http://localhost:8080/social-hub-connector/loadingData";    public static String personalHomePage = "******";    public static String weiboUsername = "*********";    public static String weiboPassword = "*********";    public static int hourbefore = 0;}

再来看一下Base64Encoder.java类,它对一些字段进行了编码的类,如下:

 1 package main.java.sina.utils; 2  3 /** 4  *  5  */ 6 public class Base64Encoder { 7     private static final char last2byte = (char) Integer.parseInt("00000011", 2); 8     private static final char last4byte = (char) Integer.parseInt("00001111", 2); 9     private static final char last6byte = (char) Integer.parseInt("00111111", 2);10     private static final char lead6byte = (char) Integer.parseInt("11111100", 2);11     private static final char lead4byte = (char) Integer.parseInt("11110000", 2);12     private static final char lead2byte = (char) Integer.parseInt("11000000", 2);13     private static final char[] encodeTable = new char[]{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'};14 15     public Base64Encoder() {16     }17     public static  String encode(byte[] from) {18         StringBuffer to = new StringBuffer((int) (from.length * 1.34) + 3);19         int num = 0;20         char currentByte = 0;21         for (int i = 0; i < from.length; i++) {22             num = num % 8;23             while (num < 8) {24                 switch (num) {25                     case 0:26                         currentByte = (char) (from[i] & lead6byte);27                         currentByte = (char) (currentByte >>> 2);28                         break;29                     case 2:30                         currentByte = (char) (from[i] & last6byte);31                         break;32                     case 4:33                         currentByte = (char) (from[i] & last4byte);34                         currentByte = (char) (currentByte << 2);35                         if ((i + 1) < from.length) {36                             currentByte |= (from[i + 1] & lead2byte) >>> 6;37                         }38                         break;39                     case 6:40                         currentByte = (char) (from[i] & last2byte);41                         currentByte = (char) (currentByte << 4);42                         if ((i + 1) < from.length) {43                             currentByte |= (from[i + 1] & lead4byte) >>> 4;44                         }45                         break;46                 }47                 to.append(encodeTable[currentByte]);48                 num += 6;49             }50         }51         if (to.length() % 4 != 0) {52             for (int i = 4 - to.length() % 4; i > 0; i--) {53                 to.append("=");54             }55         }56         return to.toString();57     }58 }

这个类中,针对新浪的一些特殊的加密规则,写的方法,这个在拼接最终的URl的时候回用到,如根据servertime+nonce两个参数来生成一串字符串加密规则:

 1 package main.java.sina.utils; 2 import java.io.File; 3 import java.io.FileReader; 4  5 import javax.script.Invocable; 6 import javax.script.ScriptEngine; 7 import javax.script.ScriptEngineManager; 8  9 /**10  * 12  */13 public class EncodeSuAndSp {14     static ScriptEngineManager mgr = new ScriptEngineManager();  15     static ScriptEngine engine = mgr.getEngineByExtension("js");16     static Invocable inv = (Invocable) engine;   17       18     public static String getEncryptedP(String password,String servertime,String nonce){19         String value1="";20         try { 21             engine.eval(new FileReader(new File("js/encrypt.js")));22             value1 = String.valueOf(inv.invokeFunction("hex_sha1",password));23             value1 = String.valueOf(inv.invokeFunction("hex_sha1",value1));24             value1 = String.valueOf(inv.invokeFunction("hex_sha1",value1+servertime+nonce));25         } catch (Exception e) {26             e.printStackTrace();27         }28         return value1;29     }30     31     32     public static String getEncodedUsername(String username){33         String value1="";34         try {35             engine.eval(new FileReader(new File("js/encrypt.js")));36             value1 = String.valueOf(inv.invokeFunction("encode",username));37             System.out.println(value1);38         } catch (Exception e) {39             e.printStackTrace();40         }41         return value1;42     }43 }
package main.java.sina.utils;import java.io.UnsupportedEncodingException;import java.net.URLDecoder;import java.net.URLEncoder;public class EncodeUtils {    public static final String encodeURL(String str,String enc) {        try {            return URLEncoder.encode(str, enc);        } catch (UnsupportedEncodingException e) {            throw new RuntimeException(e);        }    }    public static final String decodeURL(String str,String enc) {        try {            return URLDecoder.decode(str, enc);        } catch (UnsupportedEncodingException e) {            throw new RuntimeException(e);        }    }        public static String unicdoeToGB2312(String str) {        String res = null;        if(str==null ){            return "";        }        StringBuffer sb = new StringBuffer();        try {            while (str.length() > 0) {                if (str.startsWith("//u")) {                    int x = 0;                    try{                        x = Integer.parseInt(str.substring(2, 6), 16);                    }catch(Exception ex){                        x=  0;                    }                    sb.append((char) x);                    str = str.substring(6);                } else {                    sb.append(str.charAt(0));                    str = str.substring(1);                }            }            res = sb.toString();        } catch (Exception e) {            e.printStackTrace(System.err);        }        res=res.replaceAll("////r", "")            .replaceAll("////n", "")            .replaceAll("////t", "")            .replaceAll("&nbsp;", "")            .replaceAll("&gt", "")            .replaceAll("//[", "/"")            .replaceAll("//]", "/"");        return res;    }        public static String unicodeTogb2312(String str) {        String res = null;        StringBuffer sb = new StringBuffer();        try {            while (str.length() > 0) {                if (str.startsWith("//u")) {                    int x = Integer.parseInt(str.substring(2, 6), 16);                    sb.append((char) x);                    str = str.substring(6);                } else {                    sb.append(str.charAt(0));                    str = str.substring(1);                }            }            res = sb.toString();        } catch (Exception e) {            e.printStackTrace(System.err);        }        res=res.replaceAll("////r", "")                .replaceAll("////t", "")                .replaceAll("&nbsp;", "")                .replaceAll("&gt", "")               .replaceAll("////n", "");        return res;    }}

这个类很关键HttpUtils.java类,这个方法中重写了doPost()和doGet()方法.如下:

package main.java.sina.utils;import java.io.ByteArrayInputStream;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.util.ArrayList;import java.util.List;import java.util.Map;import java.util.Random;import java.util.Set;import org.apache.http.Header;import org.apache.http.HttpEntity;import org.apache.http.HttpHost;import org.apache.http.HttpResponse;import org.apache.http.HttpVersion;import org.apache.http.NameValuePair;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.HttpClient;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.client.methods.HttpUriRequest;import org.apache.http.conn.params.ConnRoutePNames;import org.apache.http.conn.params.ConnRouteParams;import org.apache.http.cookie.Cookie;import org.apache.http.entity.InputStreamEntity;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;import org.apache.http.impl.cookie.BasicClientCookie;import org.apache.http.message.BasicNameValuePair;import org.apache.http.params.BasicHttpParams;import org.apache.http.params.CoreProtocolPNames;import org.apache.http.params.HttpParams;import org.apache.http.params.HttpProtocolParams;import org.apache.http.protocol.BasicHttpContext;import org.apache.http.protocol.ExecutionContext;import org.apache.http.protocol.HTTP;import org.apache.http.protocol.HttpContext;/** * http操作相关的类 */public class HttpUtils {    /*     * params :     * url:  地址     * headers请求头部信息     * return : httpresponse响应     */    public static HttpResponse doGet(String url,Map<String,String> headers){        HttpClient client=createHttpClient();        HttpGet getMethod=new HttpGet(url);        HttpResponse response=null;                HttpContext httpContext = new BasicHttpContext();        try {            if(headers!=null && headers.keySet().size()>0){                for(String key:headers.keySet()){                    getMethod.addHeader(key, headers.get(key));                }            }                response=client.execute(getMethod);            HttpUriRequest realRequest  = (HttpUriRequest)httpContext.getAttribute(ExecutionContext.HTTP_REQUEST);            System.out.println(realRequest.getURI());        } catch (ClientProtocolException e) {            e.printStackTrace();        } catch (IOException e) {            String msg=e.getMessage();            if(msg.contains("Truncated chunk")){                System.out.println(e.getMessage() +" 数据获取不完整,需要重新获取。");            }else{                System.out.println(e.getMessage() +" 连接被拒绝,需要降低爬取频率。");            }        } catch(Exception e){        }        System.out.println(response);        return response;            }        /*     * params :     * url:  地址     * headers:请求头部信息     * params:post的请求数据     * return : httpresponse响应     */        public static HttpResponse doPost(String url,Map<String,String> headers,Map<String,String> params){        HttpClient client=createHttpClient();        HttpPost postMethod=new HttpPost(url);        HttpResponse response=null;        try {            if(headers!=null && headers.keySet().size()>0){                for(String key:headers.keySet()){                    postMethod.addHeader(key, headers.get(key));                }            }                List<NameValuePair> p=null;            if(params!=null && params.keySet().size()>0){                p=new ArrayList<NameValuePair>();                for(String key:params.keySet()){                    p.add(new BasicNameValuePair(key,params.get(key)));                }            }            if(p!=null)                postMethod.setEntity(new UrlEncodedFormEntity(p,HTTP.UTF_8));            response=client.execute(postMethod);        } catch (ClientProtocolException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }         return response;                }        //上传一个文件    public static HttpResponse doPost(String url,Map<String,String> headers,String fileName){        HttpClient client=createHttpClient();        HttpPost postMethod=new HttpPost(url);        String boundary = "";        HttpResponse response=null;        try {            if(headers!=null && headers.keySet().size()>0){                for(String key:headers.keySet()){                    postMethod.addHeader(key, headers.get(key));                    if(key.equals("Content-Type")){                        String tmp=headers.get(key);                        boundary=tmp.substring(tmp.indexOf("=")+1);                    }                }            }                File file=new File(fileName);            InputStream in=new FileInputStream(file);                        StringBuffer buffer=new StringBuffer();            buffer.append(boundary).append("/n")                  .append("Content-Disposition: form-data; name=/"pic1/"; filename=/""+file.getName()).append("/"/n")                  .append("Content-Type: image/pjpeg").append("/n")                  .append("/n");                        System.out.println(buffer.toString());                        String tmpstr=Utils.getStringFromStream(in);            tmpstr=Base64Encoder.encode(tmpstr.getBytes());            buffer.append(tmpstr).append("/n");            buffer.append(boundary+"--").append("/n");                        System.out.println(buffer.toString());                        in=new ByteArrayInputStream(buffer.toString().getBytes());                        InputStreamEntity ise=new InputStreamEntity(in,buffer.toString().getBytes().length);                          postMethod.setEntity(ise);                          response=client.execute(postMethod);        } catch (ClientProtocolException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }         return response;                }    /*     * params :     * httpresponse     * return : 响应的头部信息     */        public static List<Header> getReponseHeaders(HttpResponse response){        List<Header> headers=null;        Header[] hds=response.getAllHeaders();        if(hds!=null && hds.length>0){            headers=new ArrayList<Header>();            for(int i=0;i<hds.length;i++){                headers.add(hds[i]);            }        }                return headers;    }        /*      * params :      * headers:头部信息       * request:请求     */    public static void setHeaders(Map<String,String> headers,HttpUriRequest request){        if(headers!=null && headers.keySet().size()>0){            for(String key:headers.keySet()){                request.addHeader(key, headers.get(key));            }        }    }        /*     * params :     * httpresponse     * return : 响应的cookies值     */        public static List<Cookie> getResponseCookies(HttpResponse response){        List<Cookie> cookies=null;        Header[] hds=response.getAllHeaders();        if(hds!=null && hds.length>0){            for(int i=0;i<hds.length;i++){                if(hds[i].getName().equalsIgnoreCase("Set-Cookie")){                    if(cookies==null){                        cookies=new ArrayList<Cookie>();                    }                                         String cookiestring[]=hds[i].getValue().split(";");                    String ss[]=cookiestring[0].split("=",2);                    String cookiename=ss[0];                    String cookievalue=ss[1];                    Cookie cookie=new BasicClientCookie(cookiename,cookievalue);                    cookies.add(cookie);                }            }        }                return cookies;    }    /*     * params :     * cookies数组     * return : cookies数组组成的字符串     */    public static String setCookie2String(List<Cookie> cookies){        StringBuilder builder=null;         if(cookies!=null && cookies.size()>0){            builder=new StringBuilder();            for(int j=0;j<cookies.size();j++){                Cookie c=cookies.get(j);                builder.append(c.getName()+"="+c.getValue());                if(j!=cookies.size()-1)                    builder.append("; ");             }            return builder.toString();        }                return null;    }        /*     * 从响应中得到输入流     */    public static InputStream getInputStreamFromResponse(HttpResponse response){        if(response==null){            return null;        }        HttpEntity entity=response.getEntity();        InputStream in=null;        try {            in = entity.getContent();        } catch (IllegalStateException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }        return  in;    }        /*     * 从响应中得到字符串     */    public static String getStringFromResponse(HttpResponse response){        if(response==null){            return null;        }        InputStream in=getInputStreamFromResponse(response);        String responseText="";        if(in!=null){            responseText=Utils.getStringFromStream(in);        }        return responseText;    }        /**     * 创建支持多线程并发连接的HTTPCL
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表