1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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 :
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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | 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){ 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.