I don't know where the original source is at, however this is modified.
I've fixed a few issues as well as added a "GetAll" function which gets all of the same strings that you want. I'll post examples later.
PHP Code:
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.TreeMap;
import java.util.regex.*;
import java.util.zip.GZIPInputStream;
import java.util.zip.Inflater;
import java.util.zip.InflaterInputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class HTTPWrapper {
private Socket sock;
private PrintWriter out;
private DataInputStream in;
public String cookies;
private String lastPage;
private String userAgent;
private String proxyHost;
private int proxyPort;
private boolean proxy;
private static int timeout = 60;
public HTTPWrapper() {
sock = new Socket();
this.userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5 (.NET CLR 3.5.30729)";
}
public String Post(String url, String data) {
return Request("POST", url, "", data);
}
public String Post(String url, String data, String referrer){
return Request("POST", url, referrer, data);
}
public void ClearCookies(){
this.cookies = null;
}
public String Get(String url, String referer) {
return Request("GET", url, referer, null);
}
public String Get(String url){
return Request("GET", url, null, null);
}
public void setProxy(String host, int port) {
this.proxyHost = host;
this.proxyPort = port;
}
public void useProxy() {
this.proxy = true;
}
public void directConnect() {
this.proxy = false;
}
public static void setTimeout(int ntimeout) {
timeout = ntimeout;
}
public static int getTimeout() {
return timeout;
}
public static String inputStreamToString(InputStream in) throws IOException {
StringBuilder out = new StringBuilder();
byte[] b = new byte[4096];
for (int n; (n = in.read(b)) != -1;) {
out.append(new String(b, 0, n));
}
return out.toString();
}
private String buildRequestHeader(String method, URL url, String referer, String data) {
StringBuilder sb = new StringBuilder();
sb.append(method + " "+ url.getFile() + " HTTP/1.1\r\n");
sb.append("Host: " + url.getHost() + "\r\n");
sb.append("User-Agent: " + this.userAgent + "\r\n");
sb.append("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n");
sb.append("Accept-Language: en-gb,en;q=0.5\r\n");
sb.append("Accept-Encoding: gzip,deflate\r\n");
sb.append("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n");
sb.append("Keep-Alive: 300\r\n");
sb.append("Connection: keep-alive\r\n");
if(referer != null && referer.length() > 0) {
sb.append("Referer: " + referer + "\r\n");
//System.out.println (sb);
}
if(this.cookies != null && this.cookies.length() > 0) {
sb.append("Cookie: " + this.cookies + "\r\n");
}
if(data != null && data.length() > 0) {
sb.append("Content-Type: application/x-www-form-urlencoded\r\n");
sb.append("Content-Length: " + data.length() + "\r\n\r\n");
sb.append(data + "\r\n");
}
sb.append("\r\n");
//System.out.println(sb.toString() + "\n\n");
return sb.toString();
}
private TreeMap<String, String> parseHeader(String data) {
TreeMap<String, String> tm = new TreeMap<String, String>();
String pat = "(.*?): (.*?)$";
Matcher match = Pattern.compile(pat, Pattern.MULTILINE).matcher(data);
while(match.find()) {
tm.put(match.group(1).trim(), match.group(2).trim());
}
return tm;
}
private void parseCookies(String data) {
//Previous Cookie
StringBuilder sb;
if(cookies == null) {
sb = new StringBuilder();
} else {
sb = new StringBuilder(this.cookies);
}
//Regex Stuff
Pattern pattern = Pattern.compile("Set-Cookie: (.*?=.*?;)");
Matcher match = pattern.matcher(data);
String tmp;
String[] strIde;
String curPos;
int Start;
int End;
String nothing;
while(match.find()) {
String tmpCookie = match.group(1);
tmp = sb.toString();
strIde = tmpCookie.split("=");
String tmpString = "";
if (sb.indexOf(strIde[0]+"=") != -1)
{
//System.out.println("Temp: ALREADY CONTAINS: " + strIde[0] + "(" + sb.toString() + ")");
curPos = sb.toString();
nothing = Between(curPos, strIde[0] + "=", ";");
//System.out.println(nothing);
Start = curPos.indexOf(strIde[0]);
tmpString = curPos.substring(Start);
String tmpString2 = curPos.replace(tmpString, "");
int tmpEnd = curPos.indexOf(nothing) + nothing.length() + tmpString2.length();
End = curPos.indexOf(nothing) + nothing.length() + 1;
//System.out.println("Start: " + Start + " (" + strIde[0] + "); End: " + tmpEnd + " (" + nothing + ")" + " - " + tmpString);
sb.delete(Start, tmpEnd);
}
if(sb.indexOf(tmpCookie) == -1) {
//System.out.println("Temp:" + tmpCookie);
sb.append(tmpCookie);
}
}
this.cookies = sb.toString();
}
public String Between(String strString, String strStart, String strEnd)
{
int intBegin = strString.indexOf(strStart) + strStart.length();
int intEnd = strString.indexOf(strEnd, intBegin + 1);
return strString.substring(intBegin, intEnd);
}
public List<String> GetAll(String Input, String Start, String End){
List<String> Values = new ArrayList<String>();
int Offset = 0;
while(true){
if(Input.length() > 0 && Start.length() > 0 && End.length() > 0 && Offset < Input.length()){
int StartPos = (Input.indexOf(Start, Offset) + Start.length());
if((StartPos - Start.length()) > -1 && Input.length() >= StartPos){
int Length = (Input.indexOf(End, StartPos) - StartPos);
if(Length >-1){
Values.add(Input.substring(StartPos, Length));
Offset = StartPos + Length;
continue;
}
}
}
break;
}
return Values;
}
private String Request(String method, String url, String referer, String data) {
//Create URL Class
URL connectionURL = null;
try {
connectionURL = new URL(url);
} catch (MalformedURLException ex) {
//ex.printStackTrace();
}
//Create Header and other strings
String finalData = null;
String header = this.buildRequestHeader(method, connectionURL, referer, data);
//System.out.println("Built header:" + header);
//System.out.println(header);
//System.out.println("\n\n\n");
String[] response = null;
StringBuilder sb = new StringBuilder();
try {
//Check to see if the socket is still connected
if(!sock.isConnected() || sock.getInetAddress().getHostName() != connectionURL.getHost()) {
sock = null;
out = null;
in = null;
sock = new Socket(connectionURL.getHost(), 80);
out = new PrintWriter(sock.getOutputStream(), true);
in = new DataInputStream(sock.getInputStream());
sock.setSoTimeout(timeout * 1000);
sock.setKeepAlive(true);
//sock.setTrafficClass(0x10);
sock.setTcpNoDelay(true);
}
//Send header data
if(sock.isConnected()) {
out.print(header);
out.flush();
}
//Wait and gather response
boolean doneReadingHeader = false;
String rdata;
while ((rdata = in.readLine()) != null) {
rdata = rdata.trim();
if (rdata.length() < 1) {
break;
}
sb.append(rdata+"\n");
}
String rHeader = sb.toString();
//Parse the Header
TreeMap<String, String> tm = this.parseHeader(rHeader);
//Deal with certain content encoding
InputStream is = null;
if(tm.get("Content-Encoding") != null) {
if(tm.get("Content-Encoding").equalsIgnoreCase("gzip")) {
is = new GZIPInputStream(sock.getInputStream());
sb.append(inputStreamToString(is));
} else if(tm.get("Content-Encoding").equalsIgnoreCase("deflate")) {
is = new InflaterInputStream(sock.getInputStream(), new Inflater(true));
sb.append(inputStreamToString(sock.getInputStream()));
}
} else {
sb.append(inputStreamToString(in));
}
//Deal with what the server wants to do
if(tm.get("Connection") != null) {
if(tm.get("Connection").equalsIgnoreCase("close")) {
out.close();
in.close();
sock.close();
}
}
//Get Final Data
finalData = sb.toString();
//Parse Cookies
this.parseCookies(finalData);
} catch (UnknownHostException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
return finalData;
}
}
Results 1 to 6 of 6
Thread: [Java] HTTPWrappers
- 30 Jan. 2011 04:28am #1
[Java] HTTPWrappers
- 30 Jan. 2011 04:42am #2
Very nice
I may use these instead of the other ones I postedShh, I'm watching My little pony.
- 05 Dec. 2011 01:04am #3
i get a 403 error when i try and use this
- 05 Dec. 2011 01:19pm #4
Try going to Google and see if you get it there also.
- 05 Dec. 2011 10:33pm #5
- 05 Dec. 2011 11:05pm #6
If you still get a 403 error download Wireshark or something and take a look at the HTTP request being sent. You should be able to identify the problem relatively quickly.