Skip to content
This repository has been archived by the owner on Feb 4, 2024. It is now read-only.

Commit

Permalink
Changed the HttpClient connection to a HttpUrlConnection
Browse files Browse the repository at this point in the history
  • Loading branch information
pascal-zarrad committed Dec 28, 2016
1 parent 0a5e9ab commit e824a89
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 41 deletions.
4 changes: 0 additions & 4 deletions GCaptchaValidator.iml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.7" level="project" />
<orderEntry type="library" name="Maven: org.apache.httpcomponents:httpclient:4.4" level="project" />
<orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore:4.4" level="project" />
<orderEntry type="library" name="Maven: commons-logging:commons-logging:1.2" level="project" />
<orderEntry type="library" name="Maven: commons-codec:commons-codec:1.9" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.projectlombok:lombok:1.16.12" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
Expand Down
8 changes: 1 addition & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

<groupId>com.github.playerforcehd</groupId>
<artifactId>gcaptchavalidator</artifactId>
<version>1.0.0</version>
<version>1.1.0</version>
<packaging>jar</packaging>

<name>GCaptchaValidator</name>
Expand Down Expand Up @@ -82,12 +82,6 @@
<version>2.7</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.4</version>
<scope>compile</scope>
</dependency>
<!-- Lombok - Easy Getters and Setters -->
<dependency>
<groupId>org.projectlombok</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,16 @@
import com.github.playerforcehd.gcaptchavalidator.captchaconfiguration.CaptchaValidationConfiguration;
import com.github.playerforcehd.gcaptchavalidator.util.Callback;
import lombok.Getter;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;

/**
Expand Down Expand Up @@ -135,28 +131,29 @@ public CaptchaValidationResult fetchSync() throws IOException, CaptchaValidation
if (this.captchaValidationConfiguration.getRemoteIP() != null) {
remoteIP = this.captchaValidationConfiguration.getRemoteIP();
}
//Create HttpClient to the Google SiteVerify servers
HttpClient httpClient = HttpClients.custom()
.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
.build();
HttpPost postData = new HttpPost(GCaptchaValidator.GOOGLE_SITEVERIFY_URL);
postData.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");
List<NameValuePair> parameters = new ArrayList<>();
parameters.add(new BasicNameValuePair("secret", secret));
parameters.add(new BasicNameValuePair("response", response));
//Create HttpUrlConnection to the Google SiteVerify servers
Map<String, Object> params = new LinkedHashMap<>();
params.put("secret", secret);
params.put("response", response);
if (remoteIP != null) {
parameters.add(new BasicNameValuePair("remoteip", remoteIP));
params.put("remoteip", remoteIP);
}
postData.setEntity(new UrlEncodedFormEntity(parameters));
HttpResponse httpResponse = httpClient.execute(postData);
BufferedReader resultReader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
StringBuilder jsonResult = new StringBuilder();
String line;
while ((line = resultReader.readLine()) != null) {
jsonResult.append(line);
}
resultReader.close();
return CaptchaValidationResult.deserializeJSon(jsonResult.toString());
byte[] parsedParams = createPostData(params);
URL url = new URL(GCaptchaValidator.GOOGLE_SITEVERIFY_URL);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpURLConnection.setRequestProperty("Content-Length", String.valueOf(parsedParams.length));
httpURLConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");
httpURLConnection.setDoOutput(true);
httpURLConnection.getOutputStream().write(parsedParams);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(), "UTF-8"));
StringBuilder stringBuilder = new StringBuilder();
for (int c; (c = bufferedReader.read()) >= 0; )
stringBuilder.append((char) c);
String googleResponse = stringBuilder.toString();
bufferedReader.close();
return CaptchaValidationResult.deserializeJSon(googleResponse);
}

/**
Expand All @@ -172,4 +169,22 @@ public CaptchaValidationResult fetchSync(String response) throws IOException, Ca
this.captchaValidationConfiguration.setResponse(response);
return fetchSync();
}

/**
* Create the post request parameters from a map
*
* @param params The parameters to parse
* @return The parsed parameters as a byte array
* @throws UnsupportedEncodingException Thrown when the UTF-8 encoding is not supported
*/
private byte[] createPostData(Map<String, Object> params) throws UnsupportedEncodingException {
StringBuilder postData = new StringBuilder();
for (Map.Entry<String, Object> param : params.entrySet()) {
if (postData.length() != 0) postData.append('&');
postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
postData.append('=');
postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
}
return postData.toString().getBytes("UTF-8");
}
}

0 comments on commit e824a89

Please sign in to comment.