Skip to content

Instantly share code, notes, and snippets.

@qfrank
Last active November 17, 2015 03:44
Show Gist options
  • Save qfrank/b5dc343eb30bf4b162e1 to your computer and use it in GitHub Desktop.
Save qfrank/b5dc343eb30bf4b162e1 to your computer and use it in GitHub Desktop.
http post without ssl validation
package test;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.ssl.SSLContextBuilder;
import javax.net.ssl.SSLContext;
import java.security.cert.CertificateException;
/**
* User: Frank Tang <br/>
* Date: 15/11/7<br/>
* Time: 下午3:00<br/>
* Email: lovefree103@gmail.com<br/>
*/
public class TestHttpPost {
public static void main(String[] args) throws Exception {
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
@Override
public boolean isTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
}).build();
HttpClient httpClient = HttpClientBuilder.create().setSSLContext(sslContext).setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).build();
HttpPost req = new HttpPost("https://www.xxx.cn/login");
req.setEntity(new StringEntity("{phone:'xxx',password:'xxx'}", ContentType.APPLICATION_JSON));
HttpResponse response = httpClient.execute(req);
System.out.println(IOUtils.readLines(response.getEntity().getContent()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment