Skip to content

Instantly share code, notes, and snippets.

@wisaruthk
Created October 16, 2015 05:15
Show Gist options
  • Save wisaruthk/94f2d3a42f97d63019eb to your computer and use it in GitHub Desktop.
Save wisaruthk/94f2d3a42f97d63019eb to your computer and use it in GitHub Desktop.
view html and find the image to download
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package mangaloader;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.params.AuthPolicy;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.ClientConnectionRequest;
import org.apache.http.conn.ManagedClientConnection;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.scheme.SchemeSocketFactory;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.message.BasicHttpRequest;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.params.SyncBasicHttpParams;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.DefaultedHttpContext;
import org.apache.http.protocol.HttpContext;
/**
*
* @author wisaruthkea
*/
public class MainDownloader {
static String REQ1 = "http://www.cartooniverse-x.co.uk/mangaview/gantz_TH/31_323_1.html";
static String REQ2 = "http://cdn04.eboy.in.th/manga/cuhafivfdai/wqerdvipo/TH/gantz/TH/323/005.inc.dll";
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {
callByHttpClient();
//callByConnMng();
}
public static void callByHttpClient() throws Exception {
ClientConnectionManager conManager = new ThreadSafeClientConnManager();
DefaultHttpClient httpclient = new DefaultHttpClient(conManager);
//Create a local instance of cookie store
CookieStore cookieStore = new BasicCookieStore();
//Create local HTTP context
HttpContext localContext = new BasicHttpContext();
//Bind custom cookie store to the local context
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
//proxy
httpclient.getCredentialsProvider().setCredentials(new AuthScope("10.182.255.60", 8080),
new UsernamePasswordCredentials("7ELEVEN\\wisaruthkea", "dfasa121"));
HttpHost proxy = new HttpHost("10.182.255.60", 8080);
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
List authPrefs = new ArrayList(2);
authPrefs.add(AuthPolicy.DIGEST);
authPrefs.add(AuthPolicy.BASIC);
// This will exclude the NTLM authentication scheme
httpclient.getParams().setParameter(AuthPolicy.NTLM, authPrefs);
HttpUriRequest req2 = new HttpGet("http://cdn01.eboy.in.th/manga/cuhafivfdai/wqerdvipo/TH/gtoshonan14days/TH/021/018.inc.dll");
req2.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
req2.addHeader("Accept-Encoding", "gzip,deflate,sdch");
req2.addHeader("Host", "www2.cartooniverse.co.uk");
req2.addHeader("Proxy-Connection", "keep-alive");
req2.addHeader("Referer", "http://www.cartooniverse-x.co.uk/mangaview/gantz_TH/31_322_1.html");
req2.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.112 Safari/534.30");
HttpResponse httpResponse2 = httpclient.execute(req2, localContext);
HttpEntity entity = httpResponse2.getEntity();
//Write
FileOutputStream fos = new FileOutputStream(new File("d:/temp/test.jpg"));
BufferedOutputStream bos = new BufferedOutputStream(fos);
if (entity.isStreaming()) {
entity.writeTo(bos);
} else {
System.out.println("not streaming.");
}
bos.close();
fos.close();
}
private static void printEntity(HttpEntity entity) throws IOException {
entity.writeTo(System.out);
}
public static void callByConnMng() throws Exception {
HttpHost target = new HttpHost("www.cartooniverse-x.co.uk", 80, "http");
HttpHost proxy = new HttpHost("10.182.255.60", 8080);
// Register the "http" protocol scheme, it is required
// by the default operator to look up socket factories.
SchemeRegistry supportedSchemes = new SchemeRegistry();
SchemeSocketFactory sf = PlainSocketFactory.getSocketFactory();
supportedSchemes.register(new Scheme("http", 80, sf));
// Prepare parameters.
// Since this example doesn't use the full core framework,
// only few parameters are actually required.
HttpParams params = new SyncBasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setUseExpectContinue(params, false);
ClientConnectionManager clcm = new ThreadSafeClientConnManager(supportedSchemes);
HttpRequest req = new BasicHttpRequest("OPTIONS", "*", HttpVersion.HTTP_1_1);
req.addHeader("Host", target.getHostName());
HttpContext ctx = new BasicHttpContext();
System.out.println("preparing route to " + target);
HttpRoute route = new HttpRoute(target, null, supportedSchemes.getScheme(target).isLayered());
System.out.println("requesting connection for " + route);
ClientConnectionRequest connRequest = clcm.requestConnection(route, null);
ManagedClientConnection conn = connRequest.getConnection(0, null);
try {
System.out.println("opening connection");
conn.open(route, ctx, params);
System.out.println("sending request");
conn.sendRequestHeader(req);
// there is no request entity
conn.flush();
System.out.println("receiving response header");
HttpResponse rsp = conn.receiveResponseHeader();
System.out.println("----------------------------------------");
System.out.println(rsp.getStatusLine());
Header[] headers = rsp.getAllHeaders();
for (int i = 0; i < headers.length; i++) {
System.out.println(headers[i]);
}
System.out.println("----------------------------------------");
System.out.println("closing connection");
conn.close();
} finally {
if (conn.isOpen()) {
System.out.println("shutting down connection");
try {
conn.shutdown();
} catch (Exception ex) {
System.out.println("problem during shutdown");
ex.printStackTrace();
}
}
System.out.println("releasing connection");
clcm.releaseConnection(conn, -1, null);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment