SmallCrawler.java

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
package small_crawler;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SmallCrawler {
public static String readUrl(String site,String sessionID){
String s="";
try {
URL my_url = new URL(site);
URLConnection conn = my_url.openConnection();
if(sessionID!=null)
conn.setRequestProperty("Cookie", "PHPSESSID="+sessionID+";");
conn.connect();
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String strTemp = "";
while(null != (strTemp = br.readLine())){
s+=strTemp;
}
} catch (Exception ex) {
ex.printStackTrace();
}
return s;
}
public static List<Byte> readUrlBytes(String site,String sessionID){
List<Byte>content=new ArrayList<Byte>();
int i=0;
try {
URL my_url = new URL(site);
URLConnection conn = my_url.openConnection();
if(sessionID!=null)
conn.setRequestProperty("Cookie", "PHPSESSID="+sessionID+";");
conn.connect();
DataInputStream dis= new DataInputStream(conn.getInputStream());
byte b;
try{
while(true){
b = dis.readByte();
content.add(b);
i++;
}
}catch(EOFException eof){
}
} catch (Exception ex) {
ex.printStackTrace();
}
return content;
}
public static String makePostToUrl(String site,String sessionID,String data){
String s="";
try {
URL my_url = new URL(site);
URLConnection conn = my_url.openConnection();
if(sessionID!=null)
conn.setRequestProperty("Cookie", "PHPSESSID="+sessionID+";");conn.setDoOutput(true);
conn.connect();
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
//write parameters
writer.write(data);
writer.flush();
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String strTemp = "";
while(null != (strTemp = br.readLine())){
s+=strTemp;
}
} catch (Exception ex) {
ex.printStackTrace();
}
return s;
}
public static String getStringByPattern(String source,String patternText,String stop){
String strTemp = "";
String s;
Pattern p;
Matcher m;
p=Pattern.compile(patternText);
m=p.matcher(source);
if(m.find()){
int i=m.end();
while(!(s=(source.substring(i,i+1))).equals(stop)){
i++;
strTemp+=s;
}
}
return strTemp;
}
}