Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android clientprotocolexception DefaultHttpClient.execute()_Android_Httpclient - Fatal编程技术网

Android clientprotocolexception DefaultHttpClient.execute()

Android clientprotocolexception DefaultHttpClient.execute(),android,httpclient,Android,Httpclient,我使用以下代码请求Web服务器的响应。服务器发送不带标头的错误响应,这会导致ClientProtocolException。我曾尝试使用检查器,但在异常被触发之前,它们没有被调用。我无法更改服务器,因为它位于嵌入式设备ALFA router R36中。 顺便说一句,处理这个问题的任何建议:如果服务器响应格式正确,那么代码就可以完美地工作 谢谢你,唐 进一步寻找问题的解决方案,我发现了一个使用套接字请求服务器的建议。我与电脑上的浏览器结合使用,检查发送到buggy服务器和从buggy服务器接收到的

我使用以下代码请求Web服务器的响应。服务器发送不带标头的错误响应,这会导致ClientProtocolException。我曾尝试使用检查器,但在异常被触发之前,它们没有被调用。我无法更改服务器,因为它位于嵌入式设备ALFA router R36中。 顺便说一句,处理这个问题的任何建议:如果服务器响应格式正确,那么代码就可以完美地工作

谢谢你,唐


进一步寻找问题的解决方案,我发现了一个使用套接字请求服务器的建议。我与电脑上的浏览器结合使用,检查发送到buggy服务器和从buggy服务器接收到的数据,并阅读维基百科上的一篇文章,解释HTTP协议。有了这些信息,并通过使用套接字,我编写了一个非常基本的httpRequestHandler,用来处理来自错误web服务器的未命中响应

class httpSocketRequest extends AsyncTask<Integer, Integer, Integer> {
    StringBuffer respTxt = new StringBuffer("");        
    int reqCode = 0;
    int reqStatus = 0;

    protected Integer doInBackground(Integer... requestCode) {
        String ip = "192.168.2.1";
        String path = "";
        String authString = ("admin:admin");
        Socket socket = null;

        switch( reqCode = requestCode[0].intValue()){
            case Constants.HTTP_GET_STATUS_INFO:    path = "adm/status_info.asp";               break;
            case Constants.HTTP_SCAN:               path = "goform/getUsbStaBSSIDListForm";     break;
        }

        try {
            socket = new Socket( ip, 80);

            PrintWriter out = new PrintWriter( socket.getOutputStream());
            out.println( "GET http://" + ip + "/" + path + " HTTP/1.0");
            out.println( "Authorization: Basic " + Base64.encodeToString(authString.getBytes(),Base64.NO_WRAP));
            out.println( "");
            out.flush();

            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            String line;
            int lineCnt = 0;

            while((line = in.readLine()) != null){
                if( lineCnt >= 0){
                    lineCnt++;
                    if(lineCnt == 1){           // first line should start with "HTTP/1.x " followed by a 3 digit status
                        if( line.length() > 12 && line.substring(0, 6).equals("HTTP/1")){
                            int p = line.indexOf(" ");
                            reqStatus = Integer.parseInt(line.substring(p+1, p+4));
                            continue;
                        } else {                // not a well formed response
                            lineCnt = -1;       // just put everything into respTxt
                            reqStatus = 200;    // and assume everything went OK
                        }
                    } else if( lineCnt > 1){    // process rest of headers
                        if( line.length() == 0){
                            lineCnt = -1;       // done with headers
                        } else {
                            // TODO insert code to process other headers
                        }
                        continue;
                    }
                }

                respTxt.append(line + "\n");
            }

        } catch (Exception e) {
            Log.e("ALFA", "HTTPReq:Exception " + e.toString());
        } finally {
            try {
                if( socket != null) socket.close();
            } catch (IOException e) {
                Log.e("ALFA", "HTTPReq:Exception closing socket" + e.toString());
            }
        }

        return reqStatus;
    }

    protected void onPostExecute(Integer reqStatus) {
        Intent intent = new Intent(Constants.HTTP_RESPONSE);

        intent.putExtra( "reqCode", reqCode);
        intent.putExtra( "reqStatus", reqStatus);
        intent.putExtra( "rspTxt", respTxt.toString());

        getBaseContext().sendBroadcast(intent);
    }
}   

我会尝试另一个类似的库,看看它是否能以不同的方式处理它。或者最好让我手动处理这个案子。
class httpSocketRequest extends AsyncTask<Integer, Integer, Integer> {
    StringBuffer respTxt = new StringBuffer("");        
    int reqCode = 0;
    int reqStatus = 0;

    protected Integer doInBackground(Integer... requestCode) {
        String ip = "192.168.2.1";
        String path = "";
        String authString = ("admin:admin");
        Socket socket = null;

        switch( reqCode = requestCode[0].intValue()){
            case Constants.HTTP_GET_STATUS_INFO:    path = "adm/status_info.asp";               break;
            case Constants.HTTP_SCAN:               path = "goform/getUsbStaBSSIDListForm";     break;
        }

        try {
            socket = new Socket( ip, 80);

            PrintWriter out = new PrintWriter( socket.getOutputStream());
            out.println( "GET http://" + ip + "/" + path + " HTTP/1.0");
            out.println( "Authorization: Basic " + Base64.encodeToString(authString.getBytes(),Base64.NO_WRAP));
            out.println( "");
            out.flush();

            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            String line;
            int lineCnt = 0;

            while((line = in.readLine()) != null){
                if( lineCnt >= 0){
                    lineCnt++;
                    if(lineCnt == 1){           // first line should start with "HTTP/1.x " followed by a 3 digit status
                        if( line.length() > 12 && line.substring(0, 6).equals("HTTP/1")){
                            int p = line.indexOf(" ");
                            reqStatus = Integer.parseInt(line.substring(p+1, p+4));
                            continue;
                        } else {                // not a well formed response
                            lineCnt = -1;       // just put everything into respTxt
                            reqStatus = 200;    // and assume everything went OK
                        }
                    } else if( lineCnt > 1){    // process rest of headers
                        if( line.length() == 0){
                            lineCnt = -1;       // done with headers
                        } else {
                            // TODO insert code to process other headers
                        }
                        continue;
                    }
                }

                respTxt.append(line + "\n");
            }

        } catch (Exception e) {
            Log.e("ALFA", "HTTPReq:Exception " + e.toString());
        } finally {
            try {
                if( socket != null) socket.close();
            } catch (IOException e) {
                Log.e("ALFA", "HTTPReq:Exception closing socket" + e.toString());
            }
        }

        return reqStatus;
    }

    protected void onPostExecute(Integer reqStatus) {
        Intent intent = new Intent(Constants.HTTP_RESPONSE);

        intent.putExtra( "reqCode", reqCode);
        intent.putExtra( "reqStatus", reqStatus);
        intent.putExtra( "rspTxt", respTxt.toString());

        getBaseContext().sendBroadcast(intent);
    }
}