String protocol = "https://";
String url = "everleaf.blogspot.com";
int maxretry = 2;
String proxy = "0"; //ganti dengan ip:port jika ingin menggunakan socks5
String postdata = URLEncoder.encode("data1=foor&data2=bar&data3=foo", "UTF-8"); //encode ke urlencode
CurlPost curlpost = new CurlPost(protocol, url, url, maxretry, proxy);
curlpost.SetStart();
curlpost.SetHeader("User-Agent", "Mozilla Gecko");
curlpost.SetHeader("Cookie", "yourcookie=here");
curlpost.SetHeader("Content-Type", "application/x-www-form-urlencoded");
curlpost.SetHeader("Upgrade-Insecure-Requests", "1");
curlpost.SetPostData(postdata);
List<String> responsecurlpost = curlpost.SetConnect();
System.out.println("ResponseCode: " + responsecurlpost.get(0));
System.out.println("ResponseCookie: " + responsecurlpost.get(1));
System.out.println("ResponseUri: " + responsecurlpost.get(2));
System.out.println("ResponseContent: " + responsecurlpost.get(3));
Penjelasan untuk beberapa response dan type datanya :
(String) responsecurlpost.get(0) = Response Code
(String) responsecurlpost.get(1) = Response Cookie
(String) responsecurlpost.get(2) = Response Uri
(String) responsecurlpost.get(3) = Response Content
Dibawah ini kode java class untuk request http post, simpan dengan nama CurlPost.java :
import java.io.BufferedReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.HttpCookie;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.nio.charset.StandardCharsets;
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 CurlPost {
private final String useragent = new RandomUA().Random();
private HttpURLConnection curlpost;
private InputStream curlpostinputstream;
private OutputStream curlpostoutputstream;
private int maxretry = 0;
private int nowretry = 0;
private String protocol;
private String urlget;
private String urlpost;
private String postdata;
private List<String> curlpostheaders = 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 final CookieManager cm = new CookieManager();
private List<String> CurlPostRetry(List<String> headers, String postdata) throws Exception{
List<String> responseAllRetry = new ArrayList<>();
if(nowretry <= maxretry){
nowretry = nowretry + 1;
Thread.sleep(5000);
CurlPost curlpostretry = new CurlPost(protocol, urlget, urlpost, (maxretry-nowretry), proxy);
curlpostretry.SetStart();
if(headers.size() > 0){
for(String headerkeyvalx:headers){
String[] headerkeyval = headerkeyvalx.split("Adelia");
curlpostretry.SetHeader(headerkeyval[0], headerkeyval[1]);
}
}
curlpostretry.SetPostData(postdata);
responseAllRetry = curlpostretry.SetConnect();
}
else{
for(int i=0;i<=3;i++){
if(i == 2){
responseAllRetry.add(protocol+urlpost);
}
else{
responseAllRetry.add("zero"+i+"MAXRETRY");
}
}
}
return responseAllRetry;
}
public CurlPost(String protocol, String urlget, String urlpost, int maxretry, String proxy) throws IOException, Exception{
this.protocol = protocol;
this.urlget = urlget;
this.urlpost = urlpost;
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);
}
cm.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(cm);
}
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])));
curlpost = (HttpURLConnection) new URL(protocol+urlpost).openConnection(sock5);
}
else{
curlpost = (HttpURLConnection) new URL(protocol+urlpost).openConnection();
}
curlpost.setDoOutput(true);
curlpost.setDoInput(true);
}
public void SetHeader(String var, String val){
if((var.isEmpty() == false) && (val.isEmpty() == false)){
curlpost.setRequestProperty(var, val);
curlpostheaders.add(var+"Adelia"+val+"Adelia");
}
}
public void SetPostData(String postdatabody){
postdata = postdatabody;
}
public List<String> SetConnect() throws IOException, Exception{
Thread.sleep(500);
try{
byte[] data = postdata.getBytes(StandardCharsets.UTF_8);
curlpostoutputstream = curlpost.getOutputStream();
curlpostoutputstream.write(data);
curlpostoutputstream.flush();
curlpostoutputstream.close();
try{
responseCode = curlpost.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){
curlpost = (HttpURLConnection) new URL(curlpost.getHeaderField("Location")).openConnection();
if(curlpost.getRequestProperty("User-Agent") == null){
curlpost.addRequestProperty("User-Agent", useragent);
}
if(curlpost.getRequestProperty("Upgrade-Insecures-Request") == null){
curlpost.addRequestProperty("Upgrade-Insecures-Request", "1");
}
curlpost.setRequestMethod("GET");
responseCode = curlpost.getResponseCode();
}
responseUri = curlpost.getURL().toString();
List<HttpCookie> cookies = cm.getCookieStore().getCookies();
for (HttpCookie cookie : cookies) {
String tmpcookie = cookie.getName()+"="+cookie.getValue()+"; ";
if(responseCookie.toString().contains(tmpcookie) == false){
responseCookie.append(tmpcookie);
}
}
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){
curlpostinputstream = curlpost.getInputStream();
}
else{
curlpostinputstream = curlpost.getErrorStream();
}
try (BufferedReader buffer = new BufferedReader(new InputStreamReader(curlpostinputstream))) {
String line = null;
while((line = buffer.readLine()) != null){
responseData += line;
}
}
catch(java.lang.NullPointerException ex){
responseData = "notfound";
}
curlpost.disconnect();
responseAll.add(String.valueOf(responseCode));
responseAll.add(responseCookie.toString());
responseAll.add(responseUri);
responseAll.add(responseData);
}
else{
try {
responseAll = CurlPostRetry(curlpostheaders, postdata);
} catch (Exception ex) {
for(int i=0;i<=3;i++){
if(i == 2){
responseAll.add(protocol+urlget);
}
else{
responseAll.add("null"+i);
}
}
}
}
}
else{
}
}
catch(javax.net.ssl.SSLProtocolException | java.net.ProtocolException | javax.net.ssl.SSLHandshakeException ex){
protocol = "http://";
responseAll = CurlPostRetry(curlpostheaders, postdata);
}
catch(java.net.ConnectException | java.net.SocketTimeoutException ex){
responseAll = CurlPostRetry(curlpostheaders, postdata);
}
catch(java.net.SocketException ex){
responseAll = CurlPostRetry(curlpostheaders, postdata);
}
catch(java.net.UnknownHostException | java.lang.NullPointerException | java.net.MalformedURLException 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 = CurlPostRetry(curlpostheaders, postdata);
}
catch(java.net.SocketException ex){
responseAll = CurlPostRetry(curlpostheaders, postdata);
}
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("[POST]["+responseAll.get(0)+"] "+responseAll.get(2)+"[RTT]["+nowretry+"] RESPONSE DATA : "+responseAll.get(3).length());
cm.getCookieStore().removeAll();
CookieHandler.setDefault(null);
return responseAll;
}
}
untuk bagian disable ssl verifikasi : TrustedDomain.java
untuk random useragent : RandomUA.java
lainnya: Java class simple Http Get
No comments:
Post a Comment
Diharapkan berkomentar dengan sopan dan santun, terimakasih.