Underscore character in URL is making Host name=null error in Android

2.2k views Asked by At

First of all I CANNOT change that URL.... that said... I have a really dumb problem, I am getting this error:

Caused by: java.lang.IllegalArgumentException: Host name may not be null

in the code below:

            URL url = null;
            try{
               //  encodedURL = URLEncoder.encode("elbot_e.csoica.artificial-solutions.com/cgi-bin/elbot.cgi", "UTF-8");

                String urlStr = "http://elbot_e.csoica.artificial-solutions.com/cgi-bin/elbot.cgi";
                url = new URL(urlStr);
                URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
                url = uri.toURL();

            } catch(Exception e) {
            }

            HttpPost httpPost = new HttpPost(String.valueOf(url));

I have checked if the URL is valid in: http://formvalidation.io/validators/uri/ and I have discovered that the UNDERSCORE_ in the URL is messing things up and I cannot change the URL, who knows a solution or workaround for this?

2

There are 2 answers

0
anirban karak On
Try to get this code.


public class UHttpClient {

    private HttpClient httpClient = null;

    public static final int OK = 200;
    public static final int NO_CONTENT = 204;

    private static final int CONNECTION_TIMEOUT = 5000;

    /* timeout for waiting for data, since once connection is established then waiting for response*/
    private static final int SOCKET_TIMEOUT = 20000;    

    public UHttpClient(UAppInstance appInstance, String loginURL, String deviceID) 
    throws UBaseException {


        HttpParams httpParameters = new BasicHttpParams();

        // Set the timeout in milliseconds until a connection is established.
        HttpConnectionParams.setConnectionTimeout(httpParameters, CONNECTION_TIMEOUT);

        // Set the default socket timeout (SO_TIMEOUT) 
        // in milliseconds which is the timeout for waiting for data.
        HttpConnectionParams.setSoTimeout(httpParameters, SOCKET_TIMEOUT);

        //httpClient = new DefaultHttpClient(httpParameters);
        httpClient = new DefaultHttpClient();

        login(loginURL, deviceID);
    }

    /**
     * Makes the GET call
     * @param urlStr - Url string
     * @param paramNames - parameter names
     * @param paramValues - parameter values
     * @return - response body string
     * @throws UBaseException
     */
    public String executeGet(String urlStr, String[] paramNames, String[] paramValues)
    throws UBaseException {

        HttpGet httpget = null;

        try {
            int length = paramNames != null ? paramNames.length : 0;

            urlStr = length > 0 ? (urlStr + "?") : urlStr;
            for (int k = 0; k < length; k++) {
                urlStr = k > 0 ? (urlStr + "&") : urlStr;
                urlStr = urlStr + paramNames[k] + "=" + paramValues[k];
            }

            httpget = new HttpGet(urlStr);

            // Execute HTTP Get Request
            ResponseHandler<String> respHandler = new BasicResponseHandler();
            String response = httpClient.execute(httpget, respHandler);

            return response;

        } catch (SocketTimeoutException e) {
                throw new UBaseException("Connection Timeout", e);
        } catch (Exception e) {
            if(null == e.getMessage() || "".equals(e.getMessage()))
                throw new UBaseException("Unable to connect to server", e);
            else
                throw new UBaseException(e.getMessage(), e);
        }
    }

    /**
     * Makes the POST call
     * @param urlStr - Url string
     * @param paramNames - parameter names
     * @param paramValues - parameter values
     * @return - response body string
     * @throws UBaseException
     */
    public String executePost(String urlStr, String[] paramNames, String[] paramValues)
    throws UBaseException {

        HttpPost httppost = new HttpPost(urlStr);

        try {
            int length = paramNames != null ? paramNames.length : 0;
            ArrayList<NameValuePair> paramsList = new ArrayList<NameValuePair>();

            for (int k = 0; k < length; k++) {
                paramsList.add(new BasicNameValuePair(paramNames[k], paramValues[k]));
            }

            List<NameValuePair> nameValuePairs = paramsList;
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
            ResponseHandler<String> respHandler = new BasicResponseHandler();
            String response = httpClient.execute(httppost, respHandler);

            return response;

        } catch (SocketTimeoutException e) {
                throw new UBaseException("Connection Timeout", e);
        } catch (Exception e) {
            if(null == e.getMessage() || "".equals(e.getMessage()))
                throw new UBaseException("Unable to connect to server", e);
            else
                throw new UBaseException(e.getMessage(), e);
        }
    }


    protected void login(String url, String deviceID)
    throws UBaseException {
        // modified by Avishek as in desktop dev instead of distrCode it is entCode
        String[] argNames = new String[] {"userDeviceID", "loginMode", "command"};
        String[] argValues = new String[] {deviceID, "handheldLogin", "login"};

        String response = executePost(url, argNames, argValues);

        if(null != response){
            response = response.trim();
        }

        // if response message is not success that means it's a failure so raising exception 
        if("success".equals(response) == false) {
            if(!"".equals(response))
                throw new UBaseException(response, null);
            else
                throw new UBaseException("Remote login failed to '" + url + "'", null);
        }
    }
}
4
Sebastian On

First, you should not use HttpPost as it's deprecated, instead use HttpURLConnection.

Second, why not use directly the String constructor?

        String urlStr = "http://elbot_e.csoica.artificial-solutions.com/cgi-bin/elbot.cgi";
        HttpPost httpPost = new HttpPost(urlStr);

Seems like you are doing a lot of unneeded extra work and introducing some bugs by complicating things too much. The URL looks valid, so that should work.

EDIT:

You can add URL encoding if you have problems with the String:

HttpPost httpPost = new HttpPost(URLEncoding.encode(urlStr, "UTF-8"));