String protocol = "https://";
String url = "everleaf.blogspot.com";
int maxretry = 2;
String proxy = "0"; //ganti dengan ip:port jika ingin menggunakan socks5
CurlGet curlget = new CurlGet(protocol, url, maxretry, proxy);
curlget.SetStart();
curlget.SetHeader("User-Agent", "Mozilla Gecko");
curlget.SetHeader("Upgrade-Insecure-Requests", "1");
List<String> responsecurlget = curlget.SetConnect();
System.out.println("ResponseCode: " + responsecurlget.get(0));
System.out.println("ResponseCookie: " + responsecurlget.get(1));
System.out.println("ResponseUri: " + responsecurlget.get(2));
System.out.println("ResponseContent: " + responsecurlget.get(3));
Penjelasan untuk beberapa response dan type datanya :
(String) responsecurlget.get(0) = Response Code
(String) responsecurlget.get(1) = Response Cookie
(String) responsecurlget.get(2) = Response Uri
(String) responsecurlget.get(3) = Response Content
Dibawah ini kode java class untuk request http get, simpan dengan nama CurlGet.java :
import java.io.BufferedReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
public class CurlGet {
private final String useragent = new RandomUA().Random();
private HttpURLConnection curlget;
private InputStream curlgetstream;
private int maxretry = 0;
private int nowretry = 0;
private String protocol;
private String urlget;
private List<String> curlgetheaders = new ArrayList<>();
private int responseCode = 0;
private StringBuilder responseCookie = new StringBuilder();
private String responseUri = "";
private String responseData = "";
private List<String> responseAll = new ArrayList<>();
private boolean redirect = false;
private String proxy = "0";
private List<String> CurlGetRetry(List<String> headers) throws Exception{
List<String> responseAllRetry = new ArrayList<>();
if(nowretry <= maxretry){
nowretry = nowretry + 1;
Thread.sleep(5000);
CurlGet curlgetretry = new CurlGet(protocol, urlget, (maxretry-nowretry), proxy);
curlgetretry.SetStart();
if(headers.size() > 0){
for(String headerkeyvalx:headers){
String[] headerkeyval = headerkeyvalx.split("Adelia");
curlgetretry.SetHeader(headerkeyval[0], headerkeyval[1]);
}
}
responseAllRetry = curlgetretry.SetConnect();
}
else{
for(int i=0;i<=3;i++){
if(i == 2){
responseAllRetry.add(protocol+urlget);
}
else{
responseAllRetry.add("zero"+i+"MAXRETRY");
}
}
}
return responseAllRetry;
}
public CurlGet(String protocol, String urlget, int maxretry, String proxy) throws IOException{
this.protocol = protocol;
this.urlget = urlget;
this.maxretry = maxretry;
this.proxy = proxy;
HostnameVerifier allHostsValid;
try {
allHostsValid = TrustedDomain.TrustMe();
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
} catch (NoSuchAlgorithmException | KeyManagementException ex) {
Logger.getLogger(CurlGet.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void SetStart() throws MalformedURLException, IOException{
if("0".equals(proxy) == false && proxy.contains(":")){
String proxyAddress[] = proxy.split(":");
Proxy sock5 = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(proxyAddress[0], Integer.parseInt(proxyAddress[1])));
curlget = (HttpURLConnection) new URL(protocol+urlget).openConnection(sock5);
}
else{
curlget = (HttpURLConnection) new URL(protocol+urlget).openConnection();
}
curlget.setDoInput(true);
}
public void SetTimeout(int timeout){
curlget.setConnectTimeout(timeout);
}
public void SetHeader(String var, String val){
if((var.isEmpty() == false) && (val.isEmpty() == false)){
curlget.setRequestProperty(var, val);
curlgetheaders.add(var+"Adelia"+val+"Adelia");
}
}
public List<String> SetConnect() throws IOException, Exception{
Thread.sleep(100);
try{
try{
responseCode = curlget.getResponseCode();
if(responseCode != 0){
if(responseCode != HttpURLConnection.HTTP_OK){
if(responseCode == HttpURLConnection.HTTP_MOVED_TEMP ||
responseCode == HttpURLConnection.HTTP_MOVED_PERM||
responseCode == HttpURLConnection.HTTP_SEE_OTHER){
redirect = true;
}
}
if(redirect){
curlget = (HttpURLConnection) new URL(curlget.getHeaderField("Location")).openConnection();
if(curlget.getRequestProperty("User-Agent") == null){
curlget.addRequestProperty("User-Agent", useragent);
}
if(curlget.getRequestProperty("Upgrade-Insecures-Request") == null){
curlget.addRequestProperty("Upgrade-Insecures-Request", "1");
}
responseCode = curlget.getResponseCode();
}
responseUri = curlget.getURL().toString();
String headerName = null;
for (int i=1; (headerName = curlget.getHeaderFieldKey(i))!=null; i++) {
if (headerName.equals("Set-cookie")||headerName.equals("Set-Cookie")){
String cookCookie = curlget.getHeaderField(i)+"; ";
responseCookie.append(cookCookie);
}
}
if(responseCode == HttpURLConnection.HTTP_OK ||
responseCode == HttpURLConnection.HTTP_CONFLICT ||
responseCode == HttpURLConnection.HTTP_MOVED_TEMP ||
responseCode == HttpURLConnection.HTTP_MOVED_PERM||
responseCode == HttpURLConnection.HTTP_SEE_OTHER ||
responseCode == HttpURLConnection.HTTP_NOT_FOUND ||
responseCode == HttpURLConnection.HTTP_FORBIDDEN ||
responseCode == HttpURLConnection.HTTP_PAYMENT_REQUIRED ||
responseCode == HttpURLConnection.HTTP_UNAVAILABLE ||
responseCode == HttpURLConnection.HTTP_UNAUTHORIZED ||
responseCode == HttpURLConnection.HTTP_NOT_ACCEPTABLE ||
responseCode == HttpURLConnection.HTTP_BAD_METHOD ||
responseCode == HttpURLConnection.HTTP_BAD_REQUEST ||
responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR){
if(responseCode >= 200 && responseCode < 400){
curlgetstream = curlget.getInputStream();
}
else{
curlgetstream = curlget.getErrorStream();
}
try (BufferedReader buffer = new BufferedReader( new InputStreamReader(curlgetstream))) {
String line = null;
while((line = buffer.readLine()) != null){
responseData += line;
}
}
catch(java.lang.NullPointerException ex){
responseData = "notfound";
}
curlget.disconnect();
responseAll.add(String.valueOf(responseCode));
responseAll.add(responseCookie.toString());
responseAll.add(responseUri);
responseAll.add(responseData);
}
else{
try {
responseAll = CurlGetRetry(curlgetheaders);
} catch (Exception ex) {
for(int i=0;i<=3;i++){
if(i == 2){
responseAll.add(protocol+urlget);
}
else{
responseAll.add("null"+i);
}
}
}
}
}
else{
for(int i=0;i<=3;i++){
if(i == 2){
responseAll.add(protocol+urlget);
}
else{
responseAll.add("null"+i);
}
}
}
}
catch(javax.net.ssl.SSLProtocolException | java.net.ProtocolException | javax.net.ssl.SSLHandshakeException ex){
protocol = "http://";
responseAll = CurlGetRetry(curlgetheaders);
}
catch(java.net.ConnectException | java.net.SocketTimeoutException ex){
responseAll = CurlGetRetry(curlgetheaders);
}
catch(java.net.SocketException ex){
responseAll = CurlGetRetry(curlgetheaders);
}
catch(java.net.UnknownHostException | java.lang.NullPointerException | java.net.MalformedURLException | javax.net.ssl.SSLException ex){
for(int i=0;i<=3;i++){
if(i == 2){
responseAll.add(protocol+urlget);
}
else{
responseAll.add("null"+i);
}
}
}
}
catch(java.net.ConnectException | java.net.SocketTimeoutException ex){
responseAll = CurlGetRetry(curlgetheaders);
}
catch(java.net.SocketException ex){
responseAll = CurlGetRetry(curlgetheaders);
}
catch(java.net.UnknownHostException | java.net.MalformedURLException ex){
for(int i=0;i<=3;i++){
if(i == 2){
responseAll.add(protocol+urlget);
}
else{
responseAll.add("null"+i);
}
}
}
System.out.println("[GET]["+responseAll.get(0)+"] [PROXY]"+proxy+" "+responseAll.get(2)+"[RTT]["+nowretry+"] RESPONSE DATA : "+responseAll.get(3).length());
return responseAll;
}
}
untuk bagian disable ssl verifikasi : TrustedDomain.java
untuk random useragent : RandomUA.java
lainnya : Java class simple Http Post
No comments:
Post a Comment
Diharapkan berkomentar dengan sopan dan santun, terimakasih.