I am trying to execute the following link using following code:
class httpget{
HttpGet httpGet=null;
public void linkexecute(){
String url="http://<server>/<path>/action=send&msg=new message";
httpGet= new HttpGet(url开发者_运维问答); // line 1
....
}
at line 1 it is giving error "Illegal arguement exception"
java.lang.IllegalArgumentException: Illegal character in query at index 77: http://<server>/<path>/sms.json?action=send&msg=new message
at java.net.URI.create(URI.java:970)
at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:75)
at com.sms.login.LoginService.sendSms(LoginService.java:143)
Whereas there's no error for given below URL which has no gaps in words of "msg="
String url="http://<server>/<path>/action=send&msg=newmessage";
How can I resolve the issue of gap in words in URL?
Here, i give you one function that will remove all invalid characters from the url . Please pass your url in this function and you will get a new url with encoded strings.
public static String convertURL(String str) {
String url = null;
try{
url = new String(str.trim().replace(" ", "%20").replace("&", "%26")
.replace(",", "%2c").replace("(", "%28").replace(")", "%29")
.replace("!", "%21").replace("=", "%3D").replace("<", "%3C")
.replace(">", "%3E").replace("#", "%23").replace("$", "%24")
.replace("'", "%27").replace("*", "%2A").replace("-", "%2D")
.replace(".", "%2E").replace("/", "%2F").replace(":", "%3A")
.replace(";", "%3B").replace("?", "%3F").replace("@", "%40")
.replace("[", "%5B").replace("\\", "%5C").replace("]", "%5D")
.replace("_", "%5F").replace("`", "%60").replace("{", "%7B")
.replace("|", "%7C").replace("}", "%7D"));
}catch(Exception e){
e.printStackTrace();
}
return url;
}
You should definitely use URLEncoder.encode(String, String)
I think the issue is with parametrs of URL. Instead of Encoding or replacing whole URL, just encode the parametres like this:
Url: http://myHost.com/mail.do?address=New York city@&time=12$3;
right url:http://myHost.com/mail.do? + UrlEncode(address) + "&" + UrlEncode(time);
Another example is:
Map<String, String> params = new HashMap<String, String>();
params.put("email", URLEncoder.encode(loginStr));
params.put("pass", URLEncoder.encode(passwStr));
Model.doAuthUser(params, userCallback);
Model.doAuthUser loks like following:
String url = "http://myHost.com/mail.do";
if (params != null) {
url += "?";
Boolean beginAddParams = true;
for (Entry<String, String> entryParams : params.entrySet()) {
if (!beginAddParams) {
url +="&";
} else {
beginAddParams = false;
}
url += entryParams.getKey() + "=" + entryParams.getValue();
}
And use the url
wherever you want.
i think you should use %20 instead of the space
The URLEncoder did not work out for me when I was posting variables to my webservice, because it was replacing all the parameters. I ended up using
URI uri = new URI("http", "server", "/path", "action=send&msg=newmessage", null);
String url = uri.toASCIIString();
and later on parsing my downloaded data using httpPost with the httpClient ...
String xml = null;
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
I assume that httpGet method will work the same way.
精彩评论