URL Encoding with httpclient - java

I have a list of URLs which I need to get the content of.
The URL is with special characters and thus needs to be encoded.
I use Commons HtpClient to get the content.
when I use:
GetMethod get = new GetMethod(url);
I get a " Invalid "illegal escape character" exception.
when I use
GetMethod get = new GetMethod();
get.setURI(new URI(url.toString(), false, "UTF-8"));
I get 404 when trying to get the page, because a space is turned to %2520 instead of just %20.
I've seen many posts about this problem, and most of them advice to build the URI part by part. The problem is that it's a given list of URLs, not a one that I can handle manually.
Any other solution for this problem?
thanks.

What if you create a new URL object from it's string like URL urlObject = new URL(url), then do urlObject.getQuery() and urlObject.getPath() to split it right, parse the Query Params into a List or a Map or something and do something like:
EDIT: I just found out that HttpClient Library has a URLEncodedUtils.parse() method which you can use easily with the code provided below. I'll edit it to fit, however is untested.
With Apache HttpClient it would be something like:
URI urlObject = new URI(url,"UTF-8");
HttpClient httpclient = new DefaultHttpClient();
List<NameValuePair> formparams = URLEncodedUtils.parse(urlObject,"UTF-8");
UrlEncodedFormEntity entity;
entity = new UrlEncodedFormEntity(formparams);
HttpPost httppost = new HttpPost(urlObject.getPath());
httppost.setEntity(entity);
httppost.addHeader("Content-Type","application/x-www-form-urlencoded");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity2 = response.getEntity();
With Java URLConnection it would be something like:
// Iterate over query params from urlObject.getQuery() like
while(en.hasMoreElements()){
String paramName = (String)en.nextElement(); // Iterator over yourListOfKeys
String paramValue = yourMapOfValues.get(paramName); // replace yourMapOfNameValues
str = str + "&" + paramName + "=" + URLEncoder.encode(paramValue);
}
try{
URL u = new URL(urlObject.getPath()); //here's the url path from your urlObject
URLConnection uc = u.openConnection();
uc.setDoOutput(true);
uc.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
PrintWriter pw = new PrintWriter(uc.getOutputStream());
pw.println(str);
pw.close();
BufferedReader in = new BufferedReader(new
InputStreamReader(uc.getInputStream()));
String res = in.readLine();
in.close();
// ...
}

If you need to manipulate with request URIs it is strongly advisable to use URIBuilder shipped with Apache HttpClient.

try it out
GetMethod get = new GetMethod(url.replace(" ","%20")).toASCIIString());

Please use the URLEncoder class.
I used it in an exact scenario and it worked just fine for me.
What I did is to use the URL class, to get the part that comes after the host
(for example - at www.bla.com/mystuff/bla.jpg this would be "mystuff/bla.jpg" - you should URLEncode only this part, and then consturct the URL again.
For example, if the orignal string is "http://www.bla.com/mystuff/bla foo.jpg" then:
Encode - "mystuff/bla foo.jpg" and get "mystuff/bla%20foo.jpg" and then attach this to the host and protocol parts:
"http://www.bla.com/mystuff/bla%20foo.jpg"
I hope this helps

Related

Posting to REST api using Java, specifically Android

I'm posting to the Wufoo api inside of an Android app and I am hitting a bit of a snag. My data does not seem to be formatting in a way that the server likes (or there is some other issue). Here is my code (note authkey and authpass are placeholders in the exmaple):
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
String json = "";
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("Field17", "Some Value");
json = jsonObject.toString();
StringEntity postData = new StringEntity(json, "UTF8");
httpPost.setEntity(postData);
String authorizationString = "Basic " + Base64.encodeToString(
("authkey" + ":" + "authpass").getBytes(),
Base64.NO_WRAP);
httpPost.setHeader("Content-type", "application/json");
httpPost.setHeader("Authorization", authorizationString);
HttpResponse httpResponse = httpclient.execute(httpPost);
inputStream = httpResponse.getEntity().getContent();
The response I get back from the server looks like this:
{"Success":0,"ErrorText":"Errors have been <b>highlighted<\/b> below.","FieldErrors":
[{"ID":"Field17","ErrorText":"This field is required. Please enter a value."}]}
This is the response for a failure (obviously) which leads me to believe I'm doing the authentication correctly, and that it just doesn't like my JSON string, I've looked through the API docs which are located here:
http://www.wufoo.com/docs/api/v3/entries/post/
and by all accounts this should work? Any suggestions?
I would start by looking at this line:
StringEntity postData = new StringEntity(json, "UTF8");
It's "UTF-8", not "UTF8".
Note: I would suggest you using the HTTP.UTF_8 constant in order to avoid this kind of problem again.
StringEntity postData = new StringEntity(json, HTTP.UTF_8);
The Field17 may be of specific field type other than string.
After reading the document, I think you missed the point. The server accepted fields parameter from http post, not from a json string.
Your problem looks like this one.
So your request should like this:
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("Field17", "Some Value"));
httpPost .setEntity(new UrlEncodedFormEntity(postParameters));
Hope this can help.
I actually figured this out, this isn't a problem with the code anyone here gave me, it's the fact that I was sending the wrong header info. This must be a quirk of the Wufoo API.
If I use the BasicNameValuePair objects like what was suggestion by R4j and I remove the line
httpPost.setHeader("Content-type", "application/json");
everything works perfectly!
Thanks for all the help and I hope this helps anyone who is having trouble with the Wufoo API and Java.

Space between http url in java

This code working fine
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://localhost:8090/Servlet/ServletFirst?to=1234&from=567&text=testtest");
If i use space between parameter value. It throws exception
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://localhost:8090/Servlet/ServletFirst?to=1234&from=567&textParam=test test");
space between test test throws error. How to resolve?
You must URL encode the parameter in your URL; use %20 instead of the space.
HttpGet request = new HttpGet("http://localhost:8090/Servlet/ServletFirst?to=1234&from=567&textParam=test%20test");
Java has a class to do do URL encoding for you, URLEncoder:
String param = "test test";
String enc = URLEncoder.encode(param, "UTF-8");
String url = "http://...&textParam=" + enc;
Just use a %20 to represent a space.
This is all part of the URL encoding: http://www.w3schools.com/tags/ref_urlencode.asp
So you would want:
HttpGet request = new HttpGet("http://localhost:8090/Servlet/ServletFirst?to=1234&from=567&text=test%20test");
Use
URLEncoder.encode("test test","UTF-8")
So change your code to
HttpGet request = new HttpGet("http://localhost:8090/Servlet/ServletFirst?to=1234&from=567&textParam="+URLEncoder.encode("test test","UTF-8"));
Note Don't Encode Whole url
URLEncoder.encode("http://...test"); // its Wrong because it will also encode the // in http://
Use %20 to indicate space in the URL, as space is not an allowed character. See the Wikipedia entry for character data in a URL.
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(
"http://localhost:8090/Servlet/ServletFirst?to=1234&from=567&textParam=test%20test");

Escaping quotes in java

I'm trying to make a webservice call where I have to pass
login.php?message=[{"email":"mikeymike#mouse.com","password":"tiger"}]
I've use backslash to escape the double quotes like this
String weblink = "login.php?message=[{\"email\":\"mikeymike#mouse.com\",\"password\":\"tiger\"}]";
But I'm still getting errors. I've tried making calls to other webservices which don't have require data with any double quotes and they work fine so I'm quite sure the problem is from this. Also I get a java.lang Exception saying
java.lang.Exception Indicates a serious configuration error.DateParseException An exception to indicate an error parsing a date string. DestroyFailedException Signals that the destroy() method failed
EDIT:
I've tried using URLEncoder and JSON object but still get an error
Here is the rest of the code
String HOST = "http://62.285.107.329/disaster/webservices/";
String weblink = "login.php?message=[{\"email\":\"mikeymike#mouse.com\",\"password\":\"tiger\"}]";
String result = callWebservice(weblink);
public String callWebservice(String weblink) {
String result = "";
try {
HttpParams httpParameters = new BasicHttpParams();
int timeoutConnection = 7500;
HttpConnectionParams.setConnectionTimeout(httpParameters,
timeoutConnection);
int timeoutSocket = 7500;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
HttpClient client = new DefaultHttpClient(httpParameters);
HttpGet request = new HttpGet();
URI link = new URI(HOST + weblink);
request.setURI(link);
HttpResponse response = client.execute(request);
BufferedReader rd = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
result = rd.readLine();
} catch (Exception e1) {
e1.printStackTrace();
result = "timeout";
}
return result;
}
Also the webservice returns a JSON object so could this also be a reason for the error?
Instead of trying this by hand and getting errors, why don't use use a combination of the JSONObject class and UrlEncoder.
JSONObject json = new JSONObject();
json.put("email","mikeymike#mouse.com" );
json.put("password", "tiger");
String s = "login.php?message=" + UrlEncoder.encode(json.toString());
You have to use %22 in place of " as in: login.php?message=[{%22email%22:%22mikeymike#mouse.com%22,%22password%22:%22tiger%22}]
" is not a valid character in an URL.
A more general solution is to use URLEncoder.encode("login.php?message=[{\"email\":\"mikeymike#mouse.com\",\"password\":\"tiger\"}]", "UTF8")
When you communicate with web services you need to URL encode your data. In your case, the url encoding would replace " with %22, but if you were to add other special characters, such as %, the encoding would capture these as well. There is a java class in the JDK for this, called URLEncoder.
So, basically, what you would do is to prepare your string using URLEncoder.encode(), like so:
String weblink = URLEncoder.encode("login.php?message=[{\"email\":\"mikeymike#mouse.com\",\"password\":\"tiger\"}]");
Now that the string is encoded, you should be able to send it along to the server and have it understand what you mean.
Apologies to all but it seems the problem was that I was trying to consume the webservice in the wrong way as it returns a JSON object.
The proper way to do this for anyone who might come across this is in the code below
String str="url";
try{
URL url=new URL(str);
URLConnection urlc=url.openConnection();
BufferedReader bfr=new BufferedReader(new InputStreamReader(urlc.getInputStream()));
String line;
while((line=bfr.readLine())!=null)
{
JSONArray jsa=new JSONArray(line);
for(int i=0;i<jsa.length();i++)
{
JSONObject jo=(JSONObject)jsa.get(i);
title=jo.getString("deal_title"); //tag name "deal_title",will return value that we save in title string
des=jo.getString("deal_description");
}
}
catch(Exeption e){
}
This answer was gotten from
How to call a json webservice through android

Removing White spaces from a string for http connection

I am trying to send a query url
String url = String.format(
"http://xxxxx/xxx/xxx&message=%s",myEditBox.getText.toString());
// Create a new HttpClient and Post Header
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httpclient.getCookieStore().addCookie(cooki);
try {
ResponseHandler<String> responseHandler = new BasicResponseHandler();
httpclient.getParams().setParameter("http.connection-manager.timeout", 15000);
String response = httpclient.execute(httppost, responseHandler);
gives me error, illegal character at query. That's white space probably. How to deal with this issue?
Best Regards
You need to encode your url.
String query = URLEncoder.encode(myEditBox.getText.toString(), "utf-8");
String url = "http://stackoverflow.com/search?q=" + query;
Can you try
httpclient.setRequestProperty("Accept-Charset","UTF-8");
url="http://xxxxx/xxx/xxx&message="+URLEncoder.encode(myEditBox.getText.toString(), "UTF-8");
Try .trim() while get value from edittext.
May be whitespace come from edittext and also use "utf-8".
see below code.
String value = URLEncoder.encode(myEditBox.getText.toString().trim(), "utf-8");
String url = "http://xxxxx/xxx/xxx&message=%s" + value;

Apache HttpClient HttpGet url with colon

I am trying to issue a get with a colon in one of my parameters but it fails with an unknownHostException here is my code:
String id = "{\"ID\":\"John Doe\"}";
String encodedID = URLEncoder.encode(id, "UTF-8").replace("+", "%20");
endpoint="https://127.0.0.1/getResourceNameToUse?id=" + encodedID;
HttpResponse response = new HttpResponse();
HttpGet httpget = new HttpGet(endpoint);
response = httpclient.execute(httpget, new RESTResponseHandler());
I get the following error:
java.net.UnknownHostException: 127.0.0.1/getResourceNameToUse?id={"ID"
So it would seem that the colon is breaking the get request. Is there a way to fix this? Why is encoding it not fixing the problem? My encoded id looks like this:
%7B%22ID%22%3A%22John%20Doe%22%7D
When I run an approximation of your code, your resulting URL is:
https://127.0.0.0/getResourceNameToUse?id=%7B%22ID%22%3A%22John%20Doe%22%7D
This is an absolutely valid URL as far as I can see. I don't see any : characters in it that would confuse the HttpClient. Let's look at the exception:
java.net.UnknownHostException: 127.0.0.0/getResourceNameToUse?id={"ID"
It looks to me that something is not using your encoded URL since it shows the {"ID as opposed to %7B%22ID%22. Any chance your code in your post isn't exactly the code you were running?
I also notice that you are going to the IP 127.0.0.0. Any chance you wanted 127.0.0.1 to connect to localhost?
I was able to fix it by essentially double url encoding the colon:
String id = "{\"ID\":\"John Doe\"}";
id = id.replace(":","%3A");
String encodedID = URLEncoder.encode(id, "UTF-8").replace("+", "%20");
endpoint="https://127.0.0.1/getResourceNameToUse?id=" + encodedID;
HttpResponse response = new HttpResponse();
HttpGet httpget = new HttpGet(endpoint);
response = httpclient.execute(httpget, new RESTResponseHandler());

Categories