httpclient3.1 can request data but httpclient4.x cannot - java

Why 3.1 can request data normally, but using 4.5 will throw Circular redirect
Do I need any additional configuration to make it compatible with 3.1?
In 4.5 I set setCircularRedirectsAllowed and setMaxRedirects but it still throws an exception
httpclient3.1:
private static Document requestData(String url, CookieStore cookieStore, String proxyIp, String proxyPort, String userAgent) {
HttpClient hc = new HttpClient();
List<Header> headers = new ArrayList<Header>();
headers.add(new Header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3"));
headers.add(new Header("Accept-Language", "zh-CN,zh;q=0.9"));
headers.add(new Header("Cache-Control", "max-age=0"));
headers.add(new Header("connection", "keep-alive"));
StringBuilder tmpcookies = new StringBuilder();
for (Cookie cookie : cookieStore.getCookies()) {
tmpcookies.append(cookie.getName()).append("=").append(cookie.getValue()).append(";");
}
headers.add(new Header("Cookie", tmpcookies.toString()));
headers.add(new Header("Host", "Host"));
headers.add(new Header("Upgrade-Insecure-Requests", "1"));
headers.add(new Header("User-Agent", userAgent));
hc.getHostConfiguration().getParams().setParameter("http.default-headers", headers);
hc.getHostConfiguration().setProxy(proxyIp, Integer.parseInt(proxyPort));
hc.getHttpConnectionManager().getParams().setConnectionTimeout(10000);
hc.getHttpConnectionManager().getParams().setSoTimeout(10000);
GetMethod get = new GetMethod(url);
try {
int code = hc.executeMethod(get);
if (code == 200) {
InputStream inputStream = get.getResponseBodyAsStream();
String result = StringUtil.inputStreamToString(inputStream);
return Jsoup.parse(result);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
httpclient4.x :
private static Document requestData(String url, CookieStore cookieStore, RequestConfig defaultRequestConfig, String userAgent, String uniSocCreCode) {
CloseableHttpClient hc = HttpClients.custom().build();
HttpGet get = new HttpGet(url);
get.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3");
get.addHeader("Accept-Language", "zh-CN,zh;q=0.9");
get.addHeader("Cache-Control", "max-age=0");
get.addHeader("connection", "keep-alive");
StringBuilder tmpcookies = new StringBuilder();
for (Cookie cookie : cookieStore.getCookies()) {
tmpcookies.append(cookie.getName()).append("=").append(cookie.getValue()).append(";");
}
get.addHeader("Cookie", tmpcookies.toString());
get.addHeader("Host", "Host");
get.addHeader("Upgrade-Insecure-Requests", "1");
get.addHeader("User-Agent", userAgent);
get.setConfig(defaultRequestConfig);
try {
CloseableHttpResponse response = hc.execute(get);
if (response.getStatusLine().getStatusCode() == 200) {
InputStream inputStream = response.getEntity().getContent();
String result = StringUtil.inputStreamToString(inputStream);
return Jsoup.parse(result);
}
} catch (Exception e) {
log.error("error", e);
}
return null;
}
error(4.x):
Caused by: org.apache.http.client.CircularRedirectException: Circular redirect to 'https://www.qcc.com/firm/a4eb893fb989c8cdd7a73809d5b65715.html'
at org.apache.http.impl.client.DefaultRedirectStrategy.getLocationURI(DefaultRedirectStrategy.java:193)
at org.apache.http.impl.client.DefaultRedirectStrategy.getRedirect(DefaultRedirectStrategy.java:223)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:126)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
... 68 common frames omitted

Still think it is a version problem
move to org.apache.httpcomponents.client5 » httpclient5 solved the problem

Related

post can't reach on target with Heroku - Fixie

I'm making REST API service with another REST API service.
When I post a request to use external REST API service, the post can't reach on target.
The target host requires static IP address and HTTP protcol, so I use Fixie on Heroku.
I get following messages on heroku.
o.apache.http.impl.execchain.RetryExec : I/O exception (org.apache.http.NoHttpResponseException) caught when processing request to {tls}->http://xxxxx.usefixie.com:80->https://my-target-host.com:443: The target server failed to respond
o.apache.http.impl.execchain.RetryExec : Retrying request to {tls}->http://xxxxx.usefixie.com:80->https://my-target-host.com:443 org.apache.http.NoHttpResponseException: my-target-host.com:443 failed to respond
Currentry, the host don't expose access log.
These are my Java code.
Code-A.
HttpPost post = new HttpPost("/action");
HttpHost targetHost = new HttpHost("my-target-host.com",443,"https");
post.setHeader("Content-Type", "application/json; charset=UTF-8");
try{
URL proxyUrl = new URL(System.getenv("FIXIE_URL"));
HttpHost proxy = new HttpHost(proxyUrl.getHost(), proxyUrl.getPort());
HttpRoutePlanner routePlanner = new HttpRoutePlanner() {
#Override
public HttpRoute determineRoute(HttpHost target, org.apache.http.HttpRequest request,
org.apache.http.protocol.HttpContext context) throws HttpException {
return new HttpRoute(target, null, new HttpHost(proxyUrl.getHost(), proxyUrl.getPort()), true);
}
};
try (CloseableHttpClient httpclient = HttpClients.custom().setRoutePlanner(routePlanner).build();) {
try {
post.setEntity(new StringEntity(CONTENT_JSON, StandardCharsets.UTF_8));
CloseableHttpResponse res = httpclient.execute(targetHost,post);
} catch (Exception e) {
e.printStackTrace();
}
}catch(Exception e){
e.printStackTrace();
}
}catch(Exception e){
e.printStackTrace();
}
Code-B.
HttpPost post = new HttpPost("/action");
HttpHost targetHost = new HttpHost("my-target-host.com",443,"https");
post.setHeader("Content-Type", "application/json; charset=UTF-8");
try{
URL proxyUrl = new URL(System.getenv("FIXIE_URL"));
String userInfo = proxyUrl.getUserInfo();
String user = userInfo.substring(0, userInfo.indexOf(':'));
String password = userInfo.substring(userInfo.indexOf(':') + 1);
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(proxyUrl.getHost(),proxyUrl.getPort()),
new UsernamePasswordCredentials(user,password));
try (CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();) {
HttpHost proxy = new HttpHost(proxyUrl.getHost(), proxyUrl.getPort());
RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
post.setConfig(config);
String encodedAuth = Base64.getEncoder().encodeToString(userInfo.getBytes());
post.setHeader("Proxy-Authorization", "Basic " + encodedAuth);
try {
post.setEntity(new StringEntity(CONTENT_JSON, StandardCharsets.UTF_8));
CloseableHttpResponse res = httpclient.execute(targetHost,post);
} catch (Exception e) {
e.printStackTrace();
}
}catch(Exception e){
e.printStackTrace();
}
}catch(Exception e){
e.printStackTrace();
}
Code-C.
HttpPost post = new HttpPost("/action");
HttpHost targetHost = new HttpHost("my-tareget-host.com",443,"https");
try{
URL proxyUrl = new URL(System.getenv("FIXIE_URL"));
String userInfo = proxyUrl.getUserInfo();
String user = userInfo.substring(0, userInfo.indexOf(':'));
String password = userInfo.substring(userInfo.indexOf(':') + 1);
DefaultHttpClient httpclient_ = new DefaultHttpClient();
try {
httpclient_.getCredentialsProvider().setCredentials(
new AuthScope(proxyUrl.getHost(), proxyUrl.getPort()),
new UsernamePasswordCredentials(user, password));
HttpHost proxy = new HttpHost(proxyUrl.getHost(), proxyUrl.getPort());
httpclient_.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
String encodedAuth = Base64.getEncoder().encodeToString(userInfo.getBytes());
post.setEntity(new StringEntity(CONTENT_JSON, StandardCharsets.UTF_8));
post.setHeader("Proxy-Authorization", "Basic " + encodedAuth);
HttpResponse rsp = httpclient_.execute(targetHost, post);
} finally {
httpclient_.getConnectionManager().shutdown();
}
}catch(Exception e){
e.printStackTrace();
}
These code cause same result.
On same environment, this ruby code is reached.
RestClient.proxy = ENV['FIXIE_URL'] if ENV['FIXIE_URL']
RestClient.post("https://my-target-host.com/action", CONTENT_JSON, {
'Content-Type' => 'application/json; charset=UTF-8'
})
What's happend on access? What should I modify on Java?

Java (HttpPost, HttpGet, HttpPut, HttpPatch) methods in C#

Following is the code snippet in JAVA.
I want to do the similar thing in C#, Metro apps.
public static String callWebservice(String paramJson) throws Exception {
...
...
DefaultHttpClient httpclient = new DefaultHttpClient();
...
if (requestType.equals("POST") ) {
HttpPost httpPostReq = new HttpPost(url);
StringEntity se = new StringEntity(jsonParam.toString());
se.setContentType("application/json;charset=UTF-8");
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json;charset=UTF-8"));
httpPostReq.setEntity(se);
httpresponse = httpclient.execute(httpPostReq);
}
else if (requestType.equals("GET") ) {
HttpGet httpGetReq = new HttpGet(url);
...
}
else if (requestType.equals("PUT") ) {
HttpPut httpPutReq = new HttpPut(url);
....
}
else if (requestType.equals("PATCH") ) {
HttpPatch httpPatchReq = new HttpPatch(url);
...
}
responseText = EntityUtils.toString(httpresponse.getEntity());
return responseText;
}
I wonder if there are separate methods of Java: ("HttpPost", "HttpGet", "HttpPut", "HttpPatch" ) in C# also.
I tried to accomplish the task using HttpClient but I'm facing errors while adding headers "Accept", "Content-Type", "X-Security-AuthKey" and throws error like:
Misued header ...
But somehow I managed to add header using "TryAddWithoutValidation".
In my C# code, I have the code snippet like following:
private async Task<string> CallHttpServiceHelper(string json)
{
try
{
...
HttpModel model = JsonConvert.DeserializeObject<HttpModel>(json);
try
{
JsonObject jsonObject = new JsonObject();
jsonObject["userName"] = JsonValue.CreateStringValue(model.paramss.userName);
jsonObject["password"] = JsonValue.CreateStringValue(model.paramss.password);
jsonObject["domain"] = JsonValue.CreateStringValue(model.paramss.domain);
jsonObject["accessKey"] = JsonValue.CreateStringValue(model.paramss.accessKey);
inputParams = jsonObject.Stringify();
}
catch (Exception ex)
{
}
url = model.url;
requestType = model.requesttype;
try
{
authenticationKey = model.authenticationKey;
}
catch (Exception ex)
{
}
if (requestType == "POST")
{
uri = new Uri(url);
data = new StringContent(inputParams, Encoding.UTF8, "application/json");
if (authenticationKey != null && authenticationKey != "")
{
data.Headers.Add("X-Security-AuthKey", authenticationKey);
}
httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
httpClient.Timeout = TimeSpan.FromMinutes(10);
var response = await httpClient.PostAsync(uri, data);
content = await response.Content.ReadAsStringAsync();
httpClient.Dispose();
}
else if (requestType == "GET")
{
uri = new Uri(url);
httpClient = new HttpClient();
if (authenticationKey != null && authenticationKey != "")
{
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("X-Security-AuthKey", authenticationKey);
}
httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
httpClient.Timeout = TimeSpan.FromMinutes(10);
var response = await httpClient.GetAsync(uri);
content = await response.Content.ReadAsStringAsync();
httpClient.Dispose();
}
else if (requestType == "PUT")
{
...
}
else if (requestType == "PATCH")
{
...
}
return content;
}
catch (Exception ex)
{
return "fail";
}
}
This code in C#, Windows RT Apps works fine for POST method. But I'm not getting the required response from webservice in GET method. In GET method I have to call WebService by passing three Headers only "Accept", "Content-Type", "X-Security-AuthKey".
I don't know what I'm doing wrong.

Storing session cookie to maintain log in session

I've been trying to get this work for a while now. Im working on an app where the user signs in with a username and password which uses a httppost request to post to the server. i get the correct response, and during the post i store the session cookie that the server gives me. (I store it in a cookie store)
But when i try to click a link on the menu ( which does a second http post) after i logged in, the servers gives me a message saying that i am not logged in. But i send the cookie that i recieved in the first post to the server in the second post, yet the server does not recognize that i am logged in. To test this more easily i used the chrome plug in "Postman" which lets you post to websites easily. The only time it worked was when i log in to the website using chrome then use Postman to do the second post, which successfully gives me a response. however, when i use Postman to log in, then also use postman to attempt the second post , again, "Not logged in". Im guessing that the cookie is not being stored properly in the app. How could i go about fixing this? I read some stuff about storing the cookies in something called "Shared Preferences", is that possibly a fix? If so, what is it and how could i store the cookies there?
public static String executeHttpPost(String url, ArrayList<NameValuePair> postParameters) throws Exception {
BufferedReader in = null;
try {
LoginLayout.httpClient = new DefaultHttpClient();
HttpPost request = new HttpPost(url);
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
request.setEntity(formEntity);
CookieStore cookiestore = LoginLayout.httpClient.getCookieStore();
HttpResponse response = LoginLayout.httpClient.execute(request);
List<Cookie> cookies = LoginLayout.httpClient.getCookieStore().getCookies();
cookiestore.addCookie(cookie);
cookie = cookies.get(0);
cookieValue = "ASPSESSIONIDCQTCRACT=" + cookiestore.getCookies();
System.out.println("The cookie" + cookieValue);
List<Cookie> cookiess = cookiestore.getCookies();
cookiee = cookies.get(0);
Header[] headers = response.getAllHeaders();
System.out.println("length" + headers.length);
for (int i=0; i < headers.length; i++) {
Header h = headers[i];
System.out.println( "Header names: "+h.getName());
System.out.println( "Header Value: "+h.getValue());
}
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
// System.out.println( mCookie);
String result = sb.toString();
return result;
}
finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} }
Here is the getter so i can access the cookie from the cookie store in the next activity
public static String getCookie(){
return cookiee.getName() +"="+cookiee.getValue();
}
Here is the second post where i try to retrieve the stored cookie, which it seems to do sucessfully, however the server doesnt recognize i am logged in
public static String executeHttpPost(String url, ArrayList<NameValuePair> postParameters) throws Exception {
BufferedReader in = null;
try {
HttpPost request = new HttpPost(url);
request.setHeader("Cookie", LoginLayout.getCookie());
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
request.setEntity(formEntity);
HttpResponse response = LoginLayout.httpClient.execute(request);
Header[] headers = response.getAllHeaders();
System.out.println("length" + headers.length);
for (int i=0; i < headers.length; i++) {
Header h = headers[i];
System.out.println( "Header names: "+h.getName());
System.out.println( "Header Value: "+h.getValue());
}
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
//System.out.println( mCookie);
String result = sb.toString();
return result;
}
finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
You have to make sure that your HttpClient is using the same HttpContext on each request.
The CookieStore is associated with the HttpContext so create a new instance of HttpContext will create a new CookieStore as well.
The best way I found is to create a static instance of HttpContext and use it on every request.
Below I added an part of a class I'm using in my apps:
public class ApiClient {
// Constants
private final static String TAG = "ApiClient";
private final static String API_URL = "your-url";
// Data
private static ApiClient mInstance;
private HttpClient mHttpClient;
private ThreadSafeClientConnManager mConnectionManager;
private HttpPost mPost;
/*
* we need it static because otherwise it will be recreated and the session
* will be lost
*/
private static HttpContext mHttpContext;
private HttpParams mParams;
private Context mContext;
public ApiClient(Context pContext) {
mParams = new BasicHttpParams();
mContext = pContext;
if (null == mHttpContext) {
CookieStore cookieStore = new BasicCookieStore();
mHttpContext = new BasicHttpContext();
mHttpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
}
ConnManagerParams.setMaxTotalConnections(mParams, 300);
HttpProtocolParams.setVersion(mParams, HttpVersion.HTTP_1_1);
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
mConnectionManager = new ThreadSafeClientConnManager(mParams, schemeRegistry);
mHttpClient = new DefaultHttpClient(mConnectionManager, mParams);
}
public static ApiClient getInstance(Context pContext) {
if (null == mInstance) {
return (mInstance = new ApiClient(pContext));
} else {
return mInstance;
}
}
public void testPOST() {
List<NameValuePair> requestParams = new ArrayList<NameValuePair>();
requestParams.add(new BasicNameValuePair("param1", "value1"));
requestParams.add(new BasicNameValuePair("param2", "value2"));
mPost = new HttpPost(API_URL);
try {
mPost.setEntity(new UrlEncodedFormEntity(requestParams, HTTP.UTF_8));
HttpResponse responsePOST = mHttpClient.execute(mPost, mHttpContext);
HttpEntity resEntity = responsePOST.getEntity();
String result = EntityUtils.toString(resEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
To test it try setting the correct API_URL and
ApiClient api = ApiClient.getInstance(somContext);
api.testPOST();

HTTP POST request with JSON String in JAVA

I have to make a http Post request using a JSON string I already have generated.
I tried different two different methods :
1.HttpURLConnection
2.HttpClient
but I get the same "unwanted" result from both of them.
My code so far with HttpURLConnection is:
public static void SaveWorkflow() throws IOException {
URL url = null;
url = new URL(myURLgoeshere);
HttpURLConnection urlConn = null;
urlConn = (HttpURLConnection) url.openConnection();
urlConn.setDoInput (true);
urlConn.setDoOutput (true);
urlConn.setRequestMethod("POST");
urlConn.setRequestProperty("Content-Type", "application/json");
urlConn.connect();
DataOutputStream output = null;
DataInputStream input = null;
output = new DataOutputStream(urlConn.getOutputStream());
/*Construct the POST data.*/
String content = generatedJSONString;
/* Send the request data.*/
output.writeBytes(content);
output.flush();
output.close();
/* Get response data.*/
String response = null;
input = new DataInputStream (urlConn.getInputStream());
while (null != ((response = input.readLine()))) {
System.out.println(response);
input.close ();
}
}
My code so far with HttpClient is:
public static void SaveWorkflow() {
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(myUrlgoeshere);
StringEntity input = new StringEntity(generatedJSONString);
input.setContentType("application/json;charset=UTF-8");
postRequest.setEntity(input);
input.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json;charset=UTF-8"));
postRequest.setHeader("Accept", "application/json");
postRequest.setEntity(input);
HttpResponse response = httpClient.execute(postRequest);
BufferedReader br = new BufferedReader(
new InputStreamReader((response.getEntity().getContent())));
String output;
while ((output = br.readLine()) != null) {
System.out.println(output);
}
httpClient.getConnectionManager().shutdown();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Where generated JsonString is like this:
{"description":"prova_Process","modelgroup":"","modified":"false"}
The response I get is:
{"response":false,"message":"Error in saving the model. A JSONObject text must begin with '{' at 1 [character 2 line 1]","ids":[]}
Any idea please?
Finally I managed to find the solution to my problem ...
public static void SaveWorkFlow() throws IOException
{
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost post = new HttpPost(myURLgoesHERE);
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("task", "savemodel"));
params.add(new BasicNameValuePair("code", generatedJSONString));
CloseableHttpResponse response = null;
Scanner in = null;
try
{
post.setEntity(new UrlEncodedFormEntity(params));
response = httpClient.execute(post);
// System.out.println(response.getStatusLine());
HttpEntity entity = response.getEntity();
in = new Scanner(entity.getContent());
while (in.hasNext())
{
System.out.println(in.next());
}
EntityUtils.consume(entity);
} finally
{
in.close();
response.close();
}
}
Another way to achieve this is as shown below:
public static void makePostJsonRequest(String jsonString)
{
HttpClient httpClient = new DefaultHttpClient();
try {
HttpPost postRequest = new HttpPost("Ur_URL");
postRequest.setHeader("Content-type", "application/json");
StringEntity entity = new StringEntity(jsonString);
postRequest.setEntity(entity);
long startTime = System.currentTimeMillis();
HttpResponse response = httpClient.execute(postRequest);
long elapsedTime = System.currentTimeMillis() - startTime;
//System.out.println("Time taken : "+elapsedTime+"ms");
InputStream is = response.getEntity().getContent();
Reader reader = new InputStreamReader(is);
BufferedReader bufferedReader = new BufferedReader(reader);
StringBuilder builder = new StringBuilder();
while (true) {
try {
String line = bufferedReader.readLine();
if (line != null) {
builder.append(line);
} else {
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
//System.out.println(builder.toString());
//System.out.println("****************");
} catch (Exception ex) {
ex.printStackTrace();
}
}

HttpUrlConnection PUT method doesn't work

I've done the following client application to consume REST services (a PUT method) using AppacheHttpClient (it's working) :
public class UserLogin {
private static final String URL = "http://192.168.1.236:8080/LULServices/webresources";
public static void main(String[] args) throws Exception {
final DefaultHttpClient httpclient = new DefaultHttpClient();
try {
httpclient.getCredentialsProvider().setCredentials(
new AuthScope("localhost", 8080),
new UsernamePasswordCredentials("xxxxx", "xxxxx"));
HttpPut httpPut = new HttpPut(URL + "/services.users/login");
HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), 10000);
httpPut.addHeader("Content-type", "multipart/form-data");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("login", "xxxxx"));
nameValuePairs.add(new BasicNameValuePair("password", "xxxxx"));
httpPut.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httpPut);
try {
HttpEntity entity = response.getEntity();
String putResponse = EntityUtils.toString(entity);
System.out.println("Login successful! Secret id: " + putResponse);
EntityUtils.consume(entity);
} finally {
httpPut.releaseConnection();
}
} finally {
httpclient.getConnectionManager().shutdown();
}
}
}
Now I want to do the same, using HttpUrlConnection, but is not working:
public class PUTmethod {
public static void main(String[] args) throws Exception
{
HttpURLConnection urlConnection = null;
try {
String webPage = "http://localhost:8080/LULServices/webresources/services.users/login";
Authenticator myAuth = new Authenticator()
{
final static String USERNAME = "xxxxx";
final static String PASSWORD = "xxxxx";
#Override
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(USERNAME, PASSWORD.toCharArray());
}
};
Authenticator.setDefault(myAuth);
URL urlToRequest = new URL(webPage);
urlConnection = (HttpURLConnection) urlToRequest.openConnection();
urlConnection.setReadTimeout(10000);
urlConnection.setConnectTimeout(15000);
urlConnection.setRequestMethod("PUT");
urlConnection.setRequestProperty("Content-type", "multipart/form-data");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("login", "xxxxx"));
nameValuePairs.add(new BasicNameValuePair("password", "xxxxx"));
OutputStream out = urlConnection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
writer.write(getQuery(nameValuePairs));
writer.close();
out.close();
urlConnection.connect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
System.out.println("Failure processing URL");
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
}
public static String getQuery(List<NameValuePair> params) throws UnsupportedEncodingException
{
StringBuilder result = new StringBuilder();
boolean first = true;
for (NameValuePair pair : params)
{
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(pair.getName(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
}
System.out.println(result.toString());
return result.toString();
}
}
By not working I mean that no errors appear, but the PUT doesn't work, like when I use the ApacheHttpClient solution. What is wrong with my code?
Thanks.
Try calling urlConnection.getResponseCode(); after urlConnection.connect(); to force a flush of the underlying outputstream and reading the inputsream.
Try set
urlConnection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
after
urlConnection.setRequestMethod("PUT");

Categories