0

i call a URL using Java from URLConnection

http://192.168.2.107/cgi-bin/mediaFileFind.cgi?action=findFile&object=27891384&condition.Channel=0&conditon.Dir[0]="/mnt/sd"&condition.StartTime=2014-8-1 00:00:00&condition.EndTime=2014-8-31 23:59:59

but when i am capture HTTP transmission using wire-shark. wire-shark capture following URL

Full request URI: http://192.168.2.107/cgi-bin/mediaFileFind.cgi?action=findFile&object=28048800&condition.Channel=0&conditon.Dir[0]="/mnt/sd"&condition.StartTime=2014-8-1 

that's mean the data loss on URL my full URL is does not send to server that's why error is generating my java Code is

 public String intilizeObject(String IP, String user, String pass, String objectID, String dir, String startTime, String endTime) {

        String result = "";
        try {
            String URL = "http://" + IP + "/cgi-bin/mediaFileFind.cgi?action=findFile&object=" + objectID + "&condition.Channel=0&conditon.Dir[0]=\"" + dir + "\"&condition.StartTime=" + startTime + "&condition.EndTime=" + endTime;           
            String authString = user + ":" + pass;
            byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
            String authStringEnc = new String(authEncBytes);
            URL url = new URL(URL);
            System.out.println(url);
            URLConnection urlConnection = url.openConnection();
            urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);

            InputStream is = urlConnection.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);

            int numCharsRead;
            char[] charArray = new char[1024];
            StringBuffer sb = new StringBuffer();
            while ((numCharsRead = isr.read(charArray)) > 0) {
                sb.append(charArray, 0, numCharsRead);
            }
            result = sb.toString();

        } catch (Exception e) {
            result = e.toString();
        }
        return result;
    } 

Help me i am not able to get what is happening there

Ashish
  • 5
  • 1
  • 4

2 Answers2

0

There is a space between 2014-8-1 and 23:59:59 in your url string.(2014-8-31 23:59:59). That may cause the problem Url must not consist of white spaces.

"A URL must not contain a literal space. It must either be encoded using the percent-encoding or a different encoding that uses URL-safe characters"

Check this answer

You must encode white spaces

Community
  • 1
  • 1
0

Try URL-encoding the startTime and endTime parameters.

String URL = "http://" + IP + "/cgi-bin/mediaFileFind.cgi?action=findFile&object=" +
             objectID + "&condition.Channel=0&conditon.Dir[0]=\"" + dir +
             "\"&condition.StartTime=" + URLEncoder.encode(startTime, "UTF-8") +
             "&condition.EndTime=" + URLEncoder.encode(endTime, "UTF-8");            

Both these parameters contain spaces in them, and in a URL spaces should be represented as %20 or +.

You may also want to consider URL-encoding the objectID and dir parameters as well.

Incidentally, there is a typo in conditon.Dir[0]: should it be condition rather than conditon?

Luke Woodward
  • 63,336
  • 16
  • 89
  • 104