I used httpclient 4.4 to send get and post request. and i just created a simpile wrapper of httpclient for easy use:
package com.u8.server.sdk;
import com.sun.net.httpserver.Headers;
import com.u8.server.log.Log;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HttpContext;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.util.StringUtils;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.CookiePolicy;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* Created by ant on 2015/10/12.
*/
public class UHttpAgent {
private int connectTimeout = 5000; //5s
private int socketTimeout = 5000; //5s
private int maxTotalConnections = 200;
private static UHttpAgent instance;
private CloseableHttpClient httpClient;
private UHttpAgent(){
}
public static UHttpAgent getInstance(){
if(instance == null){
instance = new UHttpAgent();
}
return instance;
}
public static UHttpAgent newInstance(){
return new UHttpAgent();
}
public String get(String url, Map params){
return get(url, null, params, "UTF-8");
}
public String post(String url, Map params){
return post(url, null, params, "UTF-8");
}
public String get(String url , Map headers, Map params, String encoding){
if(this.httpClient == null){
init();
}
String fullUrl = url;
String urlParams = parseGetParams(params, encoding);
if (urlParams != null)
{
if (url.contains("?"))
{
fullUrl = url + "&" + urlParams;
}
else
{
fullUrl = url + "?" + urlParams;
}
}
Log.d("the full url is "+ fullUrl);
HttpGet getReq = new HttpGet(fullUrl.trim());
getReq.setHeaders(parseHeaders(headers));
ResponseHandler responseHandler = new ResponseHandler() {
#Override
public String handleResponse(HttpResponse httpResponse) throws IOException {
HttpEntity entity = httpResponse.getEntity();
return entity != null ? EntityUtils.toString(entity) : null;
}
};
try {
String res = httpClient.execute(getReq, responseHandler);
return res;
} catch (Exception e) {
e.printStackTrace();
}finally {
getReq.releaseConnection();
}
return null;
}
public String post(String url, Map headers, Map params, String encoding){
List pairs = new ArrayList();
if(params != null){
for(String key : params.keySet()){
pairs.add(new BasicNameValuePair(key, params.get(key)));
}
}
return post(url, headers, new UrlEncodedFormEntity(pairs, Charset.forName(encoding)));
}
public String post(String url, Map headers, HttpEntity entity){
if(this.httpClient == null) {
init();
}
HttpPost httpPost = new HttpPost(url);
httpPost.setHeaders(parseHeaders(headers));
httpPost.setEntity(entity);
ResponseHandler responseHandler = new ResponseHandler() {
#Override
public String handleResponse(HttpResponse httpResponse) throws IOException {
HttpEntity entity = httpResponse.getEntity();
return entity != null ? EntityUtils.toString(entity) : null;
}
};
try {
String body = httpClient.execute(httpPost, responseHandler);
return body;
} catch (IOException e) {
e.printStackTrace();
}finally {
httpPost.releaseConnection();
}
return null;
}
private Header[] parseHeaders(Map headers){
if(null == headers || headers.isEmpty()){
return getDefaultHeaders();
}
Header[] hs = new BasicHeader[headers.size()];
int i = 0;
for(String key : headers.keySet()){
hs[i] = new BasicHeader(key, headers.get(key));
i++;
}
return hs;
}
private Header[] getDefaultHeaders(){
Header[] hs = new BasicHeader[2];
hs[0] = new BasicHeader("Content-Type", "application/x-www-form-urlencoded");
hs[1] = new BasicHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36");
return hs;
}
private String parseGetParams(Map data, String encoding){
if(data == null || data.size() keyItor = data.keySet().iterator();
while(keyItor.hasNext()){
String key = keyItor.next();
String val = data.get(key);
try {
result.append(key).append("=").append(URLEncoder.encode(val, encoding).replace("+", "%2B")).append("&");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return result.deleteCharAt(result.length() - 1).toString();
}
private void init(){
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(connectTimeout)
.setSocketTimeout(socketTimeout)
.setExpectContinueEnabled(true)
.setAuthenticationEnabled(true)
.build();
HttpRequestRetryHandler retryHandler = new HttpRequestRetryHandler() {
#Override
public boolean retryRequest(IOException e, int retryNum, HttpContext httpContext) {
if(retryNum >= 3){
return false;
}
if(e instanceof org.apache.http.NoHttpResponseException
|| e instanceof org.apache.http.client.ClientProtocolException
|| e instanceof java.net.SocketException){
return true;
}
return false;
}
};
try{
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
#Override
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
}).build();
HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
SSLConnectionSocketFactory sslFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
Registry socketFactoryRegistry = RegistryBuilder.create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", sslFactory)
.build();
PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager( socketFactoryRegistry);
connMgr.setMaxTotal(maxTotalConnections);
connMgr.setDefaultMaxPerRoute((connMgr.getMaxTotal()));
HttpClientBuilder builder = HttpClients.custom()
.setDefaultRequestConfig(requestConfig)
.setSslcontext(sslContext)
.setConnectionManager(connMgr)
.setRetryHandler(retryHandler);
this.httpClient = builder.build();
}catch (Exception e){
e.printStackTrace();
}
}
public HttpClient getHttpClient(){
return this.httpClient;
}
public void destroy(){
if(this.httpClient != null){
try{
this.httpClient.close();
this.httpClient = null;
}catch (Exception e){
e.printStackTrace();
}
}
}
}
when I use this class to send post request. something strange happened:
the first time, I send a post request to the server, it's ok
after a minutes, I send a same request to the server, it's ok too.
but after a few minutes, I send a same request, something wrong:
java.net.SocketException: Connection reset by peer: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
at org.apache.http.impl.conn.LoggingOutputStream.write(LoggingOutputStream.java:77)
at org.apache.http.impl.io.SessionOutputBufferImpl.streamWrite(SessionOutputBufferImpl.java:126)
at org.apache.http.impl.io.SessionOutputBufferImpl.flushBuffer(SessionOutputBufferImpl.java:138)
at org.apache.http.impl.io.SessionOutputBufferImpl.flush(SessionOutputBufferImpl.java:146)
at org.apache.http.impl.BHttpConnectionBase.doFlush(BHttpConnectionBase.java:175)
at org.apache.http.impl.DefaultBHttpClientConnection.flush(DefaultBHttpClientConnection.java:185)
at org.apache.http.impl.conn.CPoolProxy.flush(CPoolProxy.java:177)
at org.apache.http.protocol.HttpRequestExecutor.doSendRequest(HttpRequestExecutor.java:215)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:122)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:271)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:71)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:220)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:164)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:139)
at com.u8.server.sdk.UHttpAgent.post(UHttpAgent.java:259)
at com.u8.server.sdk.UHttpAgent.post(UHttpAgent.java:147)
at com.u8.server.sdk.baidu.BaiduSDK.verify(BaiduSDK.java:30)
at com.u8.server.web.UserAction.getLoginToken(UserAction.java:100)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
but then , I still send a same request to the server, it's ok again.
Every time I tried according to the above steps, The same thing happened.
Anyone can help me ? Thanks a lot.
The peer of your client is the server. So "Connection reset by peer" means the server reset the socket. Reset means forceably closed.
This might be because of a bug in the server. If you also wrote the server or servlet (as will be the case for a custom protocol), you need to examine the behaviour of the server to examine the cause of this. The log files of the server might provide clues.
If the server has protection against bisbehaving or malicious clients, the server might have reset the socket because it has classified your client as misbehaving. If you implemented the client protocol code it might be because of a bug in your protocol implementation. If you are using third party code for handling the communication protocol you might still be misusing it; for example by sending excessively large requests. It is not uncommon for HTTP servers to protect themselves against denial of service attacks by imposing maximum lengths for header fields and bodies, and to require that clients send data at a reasonably fast rate (without pausing for long periods). Your client might have fallen foul of these protections.
Related
I used httpclient 4.4 to send get and post request. and i just created a simpile wrapper of httpclient for easy use:
package com.u8.server.sdk;
import com.sun.net.httpserver.Headers;
import com.u8.server.log.Log;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HttpContext;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.util.StringUtils;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.CookiePolicy;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* Created by ant on 2015/10/12.
*/
public class UHttpAgent {
private int connectTimeout = 5000; //5s
private int socketTimeout = 5000; //5s
private int maxTotalConnections = 200;
private static UHttpAgent instance;
private CloseableHttpClient httpClient;
private UHttpAgent(){
}
public static UHttpAgent getInstance(){
if(instance == null){
instance = new UHttpAgent();
}
return instance;
}
public static UHttpAgent newInstance(){
return new UHttpAgent();
}
public String get(String url, Map params){
return get(url, null, params, "UTF-8");
}
public String post(String url, Map params){
return post(url, null, params, "UTF-8");
}
public String get(String url , Map headers, Map params, String encoding){
if(this.httpClient == null){
init();
}
String fullUrl = url;
String urlParams = parseGetParams(params, encoding);
if (urlParams != null)
{
if (url.contains("?"))
{
fullUrl = url + "&" + urlParams;
}
else
{
fullUrl = url + "?" + urlParams;
}
}
Log.d("the full url is "+ fullUrl);
HttpGet getReq = new HttpGet(fullUrl.trim());
getReq.setHeaders(parseHeaders(headers));
ResponseHandler responseHandler = new ResponseHandler() {
#Override
public String handleResponse(HttpResponse httpResponse) throws IOException {
HttpEntity entity = httpResponse.getEntity();
return entity != null ? EntityUtils.toString(entity) : null;
}
};
try {
String res = httpClient.execute(getReq, responseHandler);
return res;
} catch (Exception e) {
e.printStackTrace();
}finally {
getReq.releaseConnection();
}
return null;
}
public String post(String url, Map headers, Map params, String encoding){
List pairs = new ArrayList();
if(params != null){
for(String key : params.keySet()){
pairs.add(new BasicNameValuePair(key, params.get(key)));
}
}
return post(url, headers, new UrlEncodedFormEntity(pairs, Charset.forName(encoding)));
}
public String post(String url, Map headers, HttpEntity entity){
if(this.httpClient == null) {
init();
}
HttpPost httpPost = new HttpPost(url);
httpPost.setHeaders(parseHeaders(headers));
httpPost.setEntity(entity);
ResponseHandler responseHandler = new ResponseHandler() {
#Override
public String handleResponse(HttpResponse httpResponse) throws IOException {
HttpEntity entity = httpResponse.getEntity();
return entity != null ? EntityUtils.toString(entity) : null;
}
};
try {
String body = httpClient.execute(httpPost, responseHandler);
return body;
} catch (IOException e) {
e.printStackTrace();
}finally {
httpPost.releaseConnection();
}
return null;
}
private Header[] parseHeaders(Map headers){
if(null == headers || headers.isEmpty()){
return getDefaultHeaders();
}
Header[] hs = new BasicHeader[headers.size()];
int i = 0;
for(String key : headers.keySet()){
hs[i] = new BasicHeader(key, headers.get(key));
i++;
}
return hs;
}
private Header[] getDefaultHeaders(){
Header[] hs = new BasicHeader[2];
hs[0] = new BasicHeader("Content-Type", "application/x-www-form-urlencoded");
hs[1] = new BasicHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36");
return hs;
}
private String parseGetParams(Map data, String encoding){
if(data == null || data.size() keyItor = data.keySet().iterator();
while(keyItor.hasNext()){
String key = keyItor.next();
String val = data.get(key);
try {
result.append(key).append("=").append(URLEncoder.encode(val, encoding).replace("+", "%2B")).append("&");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return result.deleteCharAt(result.length() - 1).toString();
}
private void init(){
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(connectTimeout)
.setSocketTimeout(socketTimeout)
.setExpectContinueEnabled(true)
.setAuthenticationEnabled(true)
.build();
HttpRequestRetryHandler retryHandler = new HttpRequestRetryHandler() {
#Override
public boolean retryRequest(IOException e, int retryNum, HttpContext httpContext) {
if(retryNum >= 3){
return false;
}
if(e instanceof org.apache.http.NoHttpResponseException
|| e instanceof org.apache.http.client.ClientProtocolException
|| e instanceof java.net.SocketException){
return true;
}
return false;
}
};
try{
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
#Override
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
}).build();
HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
SSLConnectionSocketFactory sslFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
Registry socketFactoryRegistry = RegistryBuilder.create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", sslFactory)
.build();
PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager( socketFactoryRegistry);
connMgr.setMaxTotal(maxTotalConnections);
connMgr.setDefaultMaxPerRoute((connMgr.getMaxTotal()));
HttpClientBuilder builder = HttpClients.custom()
.setDefaultRequestConfig(requestConfig)
.setSslcontext(sslContext)
.setConnectionManager(connMgr)
.setRetryHandler(retryHandler);
this.httpClient = builder.build();
}catch (Exception e){
e.printStackTrace();
}
}
public HttpClient getHttpClient(){
return this.httpClient;
}
public void destroy(){
if(this.httpClient != null){
try{
this.httpClient.close();
this.httpClient = null;
}catch (Exception e){
e.printStackTrace();
}
}
}
}
when I use this class to send post request. something strange happened:
the first time, I send a post request to the server, it's ok
after a minutes, I send a same request to the server, it's ok too.
but after a few minutes, I send a same request, something wrong:
java.net.SocketException: Connection reset by peer: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
at org.apache.http.impl.conn.LoggingOutputStream.write(LoggingOutputStream.java:77)
at org.apache.http.impl.io.SessionOutputBufferImpl.streamWrite(SessionOutputBufferImpl.java:126)
at org.apache.http.impl.io.SessionOutputBufferImpl.flushBuffer(SessionOutputBufferImpl.java:138)
at org.apache.http.impl.io.SessionOutputBufferImpl.flush(SessionOutputBufferImpl.java:146)
at org.apache.http.impl.BHttpConnectionBase.doFlush(BHttpConnectionBase.java:175)
at org.apache.http.impl.DefaultBHttpClientConnection.flush(DefaultBHttpClientConnection.java:185)
at org.apache.http.impl.conn.CPoolProxy.flush(CPoolProxy.java:177)
at org.apache.http.protocol.HttpRequestExecutor.doSendRequest(HttpRequestExecutor.java:215)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:122)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:271)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:71)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:220)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:164)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:139)
at com.u8.server.sdk.UHttpAgent.post(UHttpAgent.java:259)
at com.u8.server.sdk.UHttpAgent.post(UHttpAgent.java:147)
at com.u8.server.sdk.baidu.BaiduSDK.verify(BaiduSDK.java:30)
at com.u8.server.web.UserAction.getLoginToken(UserAction.java:100)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
but then , I still send a same request to the server, it's ok again.
Every time I tried according to the above steps, The same thing happened.
Anyone can help me ? Thanks a lot.
The peer of your client is the server. So "Connection reset by peer" means the server reset the socket. Reset means forceably closed.
This might be because of a bug in the server. If you also wrote the server or servlet (as will be the case for a custom protocol), you need to examine the behaviour of the server to examine the cause of this. The log files of the server might provide clues.
If the server has protection against bisbehaving or malicious clients, the server might have reset the socket because it has classified your client as misbehaving. If you implemented the client protocol code it might be because of a bug in your protocol implementation. If you are using third party code for handling the communication protocol you might still be misusing it; for example by sending excessively large requests. It is not uncommon for HTTP servers to protect themselves against denial of service attacks by imposing maximum lengths for header fields and bodies, and to require that clients send data at a reasonably fast rate (without pausing for long periods). Your client might have fallen foul of these protections.
I am trying to send file using java, i have a existing python which is working which needs to be converted to java.
Below is my python code,
with io.open('test.text', 'rb') as f:
r = requests.request("POST",'http://my_url/post', data=f)
My java code
HttpURLConnection httpUrlConnection = null;
URL url = new URL("http://my_url/post");
httpUrlConnection = (HttpURLConnection) url.openConnection();
httpUrlConnection.setRequestMethod("POST");
Now i am not sure how to pass the file to the post request
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.concurrent.FutureCallback;
import org.apache.http.entity.FileEntity;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClients;
import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
public class PostFile {
public static void main(String[] aaa) throws URISyntaxException{
final URI uri = new URIBuilder("http://httpbin.org/post")
.build();
HttpPost request = new HttpPost(uri);
File f = new File("file.txt");
request.setEntity(new FileEntity(f));
CloseableHttpAsyncClient client = HttpAsyncClients.custom().build();
client.start();
client.execute(request, new FutureCallback<HttpResponse>() {
#Override
public void completed(HttpResponse httpResponse) {
System.out.println("completed: " + httpResponse);
}
#Override
public void failed(Exception e) {
System.out.println("failed");
}
#Override
public void cancelled() {
System.out.println("cancelled");
}
});
}
}
Try this
I am using a rest service which requires authentication, Below curl command is used to achieve this
curl -v --insecure --request POST "https://ip:port/login" -d IDToken1="username" -d "password" --cookie-jar cookie.txt
After authentication it creates a cookie file.
Can someone helps in creating the corresponding rest client using java.
I have used
ClientConfig config = new ClientConfig();
Client client = ClientBuilder.newClient(config);
WebTarget target = client
.target("http://hilweb05:8080/login");
Form form = new Form().param("IDToken1", "username").param("IDToken2", "password");
Response jsonAnswer = target.request()
.accept(MediaType.APPLICATION_JSON).post(Entity.form(form));
if (jsonAnswer.getStatus() != 200) {
throw new RuntimeException("Not reachable "
+ jsonAnswer.getStatus());
}
List<SomeDataClass> matList = jsonAnswer.readEntity(new GenericType<List<SomeDataClass>>() {});
for (SomeDataClass m : matList) {
System.out.println(m.getF1() + " " + m.getF2() + " "
+ m.getF3());
}
But its not working
I switched to Apache http client, with the below piece of code I am able to get the cookie.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.net.ssl.SSLContext;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.client.LaxRedirectStrategy;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
public class ApacheHttpClient {
public static void main(String[] args) throws ClientProtocolException, IOException, KeyManagementException, NoSuchAlgorithmException, KeyStoreException {
SSLContext sslContext = SSLContexts.custom()
.loadTrustMaterial((chain, authType) -> true).build();
SSLConnectionSocketFactory sslConnectionSocketFactory =
new SSLConnectionSocketFactory(sslContext, new String[]
{"SSLv2Hello", "SSLv3", "TLSv1","TLSv1.1", "TLSv1.2" }, null,
NoopHostnameVerifier.INSTANCE);
CloseableHttpClient httpclient = HttpClients.custom()
.setSSLSocketFactory(sslConnectionSocketFactory)
.build();
try {
HttpPost httpPost = new HttpPost("url/login");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("IDToken1", name));
nvps.add(new BasicNameValuePair("IDToken2", password));
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
CloseableHttpResponse response2 = httpclient.execute(httpPost);
try {
System.out.println("Status -->>> "+ response2.getStatusLine().getStatusCode());
Header[] cookieInf = response2.getHeaders("Set-Cookie");
StringBuilder strBf = new StringBuilder();
for(Header header : cookieInf)
{
strBf.append(header.getValue());
}
System.out.println("Data is "+ strBf);
HttpEntity entity2 = response2.getEntity();
// do something useful with the response body
// and ensure it is fully consumed
EntityUtils.consume(entity2);
} finally {
response2.close();
}
} finally {
httpclient.close();
}
}
}
Now I need to write the cookie in a text file, so I need help in parsing the cookie information so that it matches the cookie file generated by curl command.
With the below piece of code it works
public class JerseyClientPost {
public static void main(String[] args) {
try {
Client client = Client.create(configureClient());
final com.sun.jersey.api.client.WebResource webResource = client
.resource("https://wtc2e3enm.eng.mobilephone.net:443/login");
MultivaluedMap formData = new MultivaluedMapImpl();
formData.add("IDToken1", name);
formData.add("IDToken2", password);
try {
ClientResponse response = webResource.type(MediaType.APPLICATION_FORM_URLENCODED)
.accept(MediaType.APPLICATION_JSON_TYPE).post(ClientResponse.class, formData);
String x = response.getEntity(String.class);
System.out.println("Response String is "+ x);
} catch (com.sun.jersey.api.client.ClientHandlerException che) {
che.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static ClientConfig configureClient() {
TrustManager[] certs = new TrustManager[] { new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkServerTrusted(X509Certificate[] chain, String authType) {
}
public void checkClientTrusted(X509Certificate[] chain, String authType) {
}
} };
SSLContext ctx = null;
try {
ctx = SSLContext.getInstance("SSL");
ctx.init(null, certs, new SecureRandom());
} catch (java.security.GeneralSecurityException ex) {
}
HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory());
ClientConfig config = new DefaultClientConfig();
try {
config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES,
new HTTPSProperties(new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
}, ctx));
} catch (Exception e) {
}
return config;
}
}
but I am not able to get the cookies from response, if I use curl I am able to get the cookie by using --cookie-jar argument. Can somebody help in getting the cookie
I have acutally a problem with my Android HTTPClient.
I want to send POST data to a website and parse the content after this.
My problem is, that the POST data wasn't sent to the webserver.
I don't know why.
Here is my HTTP Util Class
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import android.os.AsyncTask;
public class HTTPHelperUtil {
private static HTTPHelperUtil instance;
private String url;
private List<NameValuePair> nameValuePairs;
private String result;
private HTTPHelperUtil() {
// nothing to do
}
public synchronized static HTTPHelperUtil getInstance() {
if (HTTPHelperUtil.instance == null) {
HTTPHelperUtil.instance = new HTTPHelperUtil();
}
return HTTPHelperUtil.instance;
}
public HTTPHelperUtil init(String url, List<NameValuePair> nameValuePairs) {
this.url = url;
this.nameValuePairs = nameValuePairs;
return HTTPHelperUtil.instance;
}
public HTTPHelperUtil start() throws ClientProtocolException, IOException {
SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
AsyncTask<List<NameValuePair>, Void, String> execute = sendPostReqAsyncTask.execute(nameValuePairs);
return HTTPHelperUtil.instance;
}
class SendPostReqAsyncTask extends AsyncTask<List<NameValuePair>, Void, String> {
#Override
protected String doInBackground(List<NameValuePair>... params) {
String res = "";
List<NameValuePair> postPairs = params[0];
System.out.println(postPairs.get(0));
System.out.println(postPairs.get(1));
if (postPairs != null && !postPairs.isEmpty()) {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
try {
httppost.setEntity(new UrlEncodedFormEntity(postPairs));
HttpResponse response;
response = httpclient.execute(httppost);
if (response != null && response.getEntity() != null) {
InputStream content = response.getEntity().getContent();
if (content != null) {
BufferedReader reader = new BufferedReader(
new InputStreamReader(content, "UTF-8"));
while (true) {
String addingSource = reader.readLine();
if (addingSource != null) {
res = addingSource;
break;
}
}
}
}
} catch (IllegalStateException e) {
return "-";
} catch (IOException e) {
return "-";
}
}
System.out.println(res);
return res;
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
HTTPHelperUtil.this.result = result;
}
}
public String getResult() {
System.out.println("Result = " + result);
return result;
}
}
There is the call from my Activity:
final String url = "http://example.com/app/mysite.php";
List<NameValuePair> postParams = new ArrayList<NameValuePair>(2);
postParams.add(new BasicNameValuePair("username", username));
postParams.add(new BasicNameValuePair("password", password));
HTTPHelperUtil.getInstance().init(url, postParams);
String result = "";
try {
HTTPHelperUtil.getInstance().start();
result = HTTPHelperUtil.getInstance().getResult();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
My test-page doesn't get the username and password via POST.
Does anyone see the mistake in my code?
Thanks
As per your code :
List postParams = new ArrayList (2);
Here you are passing 2 in the constructor which is not required while declaring the list with name value pair.
This might be the reason as rest all code seems fine.
I need to send an SMS through my Java Application. I had piece of code that used to work perfectly well where I had made use of a SMS sending site. However the site introduced captcha verification because of which my code fails. Please find the below code that I had tried. Request you to please guide me through any other alternatives that I can make use of sending SMS through Java.
package com.test;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
public class Mobiletry2
{
private final String LOGIN_URL = "http://*****.com/login.php";
private final String SEND_SMS_URL = "http://*****.com/home.php";
private final String LOGOUT_URL = "http://*****.com/logout.php?LogOut=1";
private final int MESSAGE_LENGTH = 10;
private final int MOBILE_NUMBER_LENGTH = 140;
private final int PASSWORD_LENGTH = 10;
private String mobileNo;
private String password;
private DefaultHttpClient httpclient;
Mobiletry2(String username,String password)
{
this.mobileNo = username;
this.password = password;
httpclient = new DefaultHttpClient();
}
public boolean isLoggedIn() throws IOException {
// User Credentials on Login page are sent using POST
// So create httpost object
HttpPost httpost = new HttpPost(LOGIN_URL);
// Add post variables to login url
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("MobileNoLogin", mobileNo));
nvps.add(new BasicNameValuePair("LoginPassword", password));
httpost.setEntity(new UrlEncodedFormEntity(nvps));
// Execute request
HttpResponse response = this.httpclient.execute(httpost);
//Check response entity
HttpEntity entity = response.getEntity();
if (entity != null) {
System.out.println("entity " + slurp(entity.getContent(), 10000000));
System.out.println("entity " + response.getStatusLine().getStatusCode());
return true;
}
return false;
}
public boolean sendSMS(String toMobile,String message) throws IOException {
HttpPost httpost = new HttpPost(SEND_SMS_URL);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("MobileNos", toMobile));
nvps.add(new BasicNameValuePair("Message", message));
httpost.setEntity(new UrlEncodedFormEntity(nvps));
HttpResponse response = this.httpclient.execute(httpost);
HttpEntity entity = response.getEntity();
if(entity != null) {
System.out.println("entity " + slurp(entity.getContent(), 10000000));
System.out.println("entity " + response.getStatusLine().getStatusCode());
return true;
}
return false;
}
public boolean logoutSMS() throws IOException {
HttpGet httpGet = new HttpGet(LOGOUT_URL);
HttpResponse response;
response = this.httpclient.execute(httpGet);
HttpEntity entity = response.getEntity();
if (entity != null) {
System.out
.println("entity " + slurp(entity.getContent(), 10000000));
System.out.println("entity "
+ response.getStatusLine().getStatusCode());
return true;
}
return false;
}
public static String slurp(final InputStream is, final int bufferSize)
{
final char[] buffer = new char[bufferSize];
final StringBuilder out = new StringBuilder();
try {
final Reader in = new InputStreamReader(is, "UTF-8");
try {
for (;;) {
int rsz = in.read(buffer, 0, buffer.length);
if (rsz < 0)
break;
out.append(buffer, 0, rsz);
}
}
finally {
in.close();
}
}
catch (UnsupportedEncodingException ex) {
/* ... */
}
catch (IOException ex) {
/* ... */
}
return out.toString();
}
/**
* #param args
*/
public static void main(String[] args) {
//Replace DEMO_USERNAME with username of your account
String username = "********";
//Replace DEMO_PASSWORD with password of your account
String password = "****";
//Replace TARGET_MOBILE with a valid mobile number
String toMobile = "****";
String toMessage = "Hello";
Mobiletry2 sMS = new Mobiletry2(username, password);
try{
if(sMS .isLoggedIn() && sMS .sendSMS(toMobile,toMessage))
{
sMS.logoutSMS();
System.out.println("Message was sent successfully " );
}
}
catch(IOException e)
{
System.out.println("Unable to send message, possible cause: " + e.getMessage());
}
}
}
Then go through the sms providing API the web service that u are using .They must be providing any alternate for over come this issue.Captcha is used for preventing automated submissions on any site .
Captcha code is generated at runtime, so it is not possible to predefine the captcha code in your application,thats the reason captcha is comes into the picture to prevent automation submissions on any site.