简体   繁体   中英

java.net.URISyntaxException when i try to post a URL

Hi am sending a url using apache HttpClient by using following code but it has been showing a exception :java.net.URISyntaxException:

Illegal character in query at index 70: http://192.155.2.144:8080/SDAX/homePage.do?actionFlag=istrict&&MSG=1|Bdrtfggf|254td|return|null|null|null

Please help me where iam doing the problem. the following code i am sending a URL

        String MSG="1|Bdrtfggf|254td|return|null|null|null" ; 
        String url="http://192.168.2.144:8080/SDAX/homePage.do?actionFlag=edistrict&&MSG="+MSG;
        System.out.println("Url is"+url);

        //String url = "http://192.168.0.6:8084/NRC_NEW_SEARCH/getVillageList.req?dist_id=1";
        //String url="http://192.168.0.85:8080/poly/web/";
        //FacesContext.getCurrentInstance().getExternalContext().redirect(url);
        //ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
        //context..redirect(url);
        HttpRequestBase request = new HttpGet(url);
        /*HttpParams params = new BasicHttpParams();
        params.setParameter("dist_id", "1");
        request.setParams(params);*/
        HttpClient httpClient = new DefaultHttpClient();
        httpClient.execute(request);

You should encode the MSG string before creating a URL from it.

String encodedMSG = URLEncoder.encode(MSG, "UTF-8")
String url="http://192.168.2.144:8080/SDAX/homePage.do?actionFlag=edistrict&&MSG="+ encodedMSG;

Edit

There won't be any problem in retrieving the data after encoding. If you have programmed this servlet homePage.do then you should use URLDecoder.decode() method in it.

  1. Since vertical bar(|) is not a valid URI character thats why You are getting URISyntaxException. Solution: As suggested by kaysush, you need to encode/decode your url. For more on this, Please check folowing url: Cannot process url with vertical/pipe bar
  2. You havn't explained what you are trying to achieve here. I hope it isn't by mistake. but As per you question, You are trying to post a url and In your code you are using HttpGet(url);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM