在Android中命令包含中文或空格时如何发送HTTP命令?

时间:2015-08-17 10:39:07

标签: java android http

我正在开发一款App,它可以与IP Camera配合使用。

以下命令格式适用于IP Camera。 的 http://192.72.1.1/config?set&value=XXXXX

我可以输入任何值并发送到IP摄像机。

当命令为 http://192.72.1.1/config?set&value=111 时,它可以发送到IP Camera。 IP摄像头也响应OK。

但是当我尝试发送包含空格或中文的命令时,它会失败。

  1. http://192.72.1.1/config?set&value=111 222

  2. http://192.72.1.1/config?set&value=二三四

  3. 代码如下:

    public static String sendRequest(URL url) {
    
            try {
                //Show the command
                Log.i("CameraCommand", url.toString()) ;
    
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection() ;
    
                urlConnection.setUseCaches(false) ;
                urlConnection.setDoInput(true) ;
    
                urlConnection.setConnectTimeout(3000) ;
                urlConnection.setReadTimeout(8000) ;
    
                urlConnection.connect() ;
    
                int responseCode = urlConnection.getResponseCode() ;
    
                Log.i("CameraControlFragment", "responseCode = " + urlConnection.getResponseCode()) ;
    
                if (responseCode != HttpURLConnection.HTTP_OK) {
    
                    return null ;
                }
    
                InputStream inputStream = urlConnection.getInputStream() ;
    
                Writer writer = new StringWriter() ;
    
                char[] buffer = new char[1024] ;
                try {
                    Reader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")) ;
                    int n ;
                    while ((n = reader.read(buffer)) != -1) {
                        writer.write(buffer, 0, n) ;
                    }
                } finally {
                    inputStream.close() ;
                }
                String string = writer.toString() ;
                Log.i("CameraControlFragment", "string = " + string) ;
    
                return string ;
    
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace() ;
            }
    
            return null ;
        }
    

    当命令包含空格或中文时,它会显示以下日志:

    08-17 18:13:18.105: W/System.err(22120): java.net.ProtocolException: Unexpected status line: HTTP/0.0 400 Bad Request
    08-17 18:13:18.105: W/System.err(22120):    at com.android.okhttp.internal.http.StatusLine.<init>(StatusLine.java:38)
    08-17 18:13:18.105: W/System.err(22120):    at com.android.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:180)
    08-17 18:13:18.105: W/System.err(22120):    at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:101)
    08-17 18:13:18.105: W/System.err(22120):    at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:628)
    08-17 18:13:18.105: W/System.err(22120):    at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:388)
    08-17 18:13:18.105: W/System.err(22120):    at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:332)
    08-17 18:13:18.106: W/System.err(22120):    at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:500)
    08-17 18:13:18.106: W/System.err(22120):    at tw.com.WIFI_CAM_R660.IPCamViewer.CameraCommand.sendRequest(CameraCommand.java:734)
    08-17 18:13:18.106: W/System.err(22120):    at tw.com.WIFI_CAM_R660.IPCamViewer.CameraCommand$SendRequest.doInBackground(CameraCommand.java:773)
    08-17 18:13:18.106: W/System.err(22120):    at tw.com.WIFI_CAM_R660.IPCamViewer.CameraCommand$SendRequest.doInBackground(CameraCommand.java:1)
    08-17 18:13:18.106: W/System.err(22120):    at android.os.AsyncTask$2.call(AsyncTask.java:292)
    08-17 18:13:18.106: W/System.err(22120):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    08-17 18:13:18.106: W/System.err(22120):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
    08-17 18:13:18.106: W/System.err(22120):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
    08-17 18:13:18.106: W/System.err(22120):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
    08-17 18:13:18.106: W/System.err(22120):    at java.lang.Thread.run(Thread.java:818)
    

    CameraCommand.java:734 是int responseCode = urlConnection.getResponseCode();

    有人知道如何发送包含空格和中文的命令吗?

    提前致谢。

2 个答案:

答案 0 :(得分:3)

您需要先对您的网址进行编码,以便可以在查询字符串中传输。

您需要为各个参数本身执行此操作。例如:

String encodedValue = URLEncoder.encode(value, "UTF-8");
String url = "http://192.72.1.1/config?set&value=" + encodedValue;
String result = sendRequest(url);

答案 1 :(得分:1)

使用Knossos提供的解决方案,或者如果你的命令值是硬编码的,你可以手动编码。

http://www.w3schools.com/tags/ref_urlencode.asp