1

我正在使用以下代码从网络服务器请求响应。服务器发送不带标头的格式错误的响应,这会导致 ClientProtocolException。我曾尝试使用检查器,但在引发异常之前不会调用它们。我无法更改服务器(它在嵌入式设备中,ALFA 路由器 R36)。处理此问题的任何建议(顺便说一句:如果服务器响应格式正确,则代码可以完美运行)

在此先感谢,吨

class httpRequestTask extends AsyncTask <Integer, Integer, Integer> {

    StringBuffer respTxt = new StringBuffer("");        
    int reqCode = 0;


    protected Integer doInBackground(Integer... requestCode) {
        Integer reqStatus = 0;
        String url = "http://192.168.2.1/";
        String authString = ("admin:admin");

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

        try {
            DefaultHttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet();
            HttpResponse response;

            request.setURI( new URI( url));           
            request.addHeader("Authorization", "Basic " + Base64.encodeToString(authString.getBytes(),Base64.NO_WRAP));

            response = client.execute(request);
            reqStatus = response.getStatusLine().getStatusCode();

            String line;
            BufferedReader in = new BufferedReader( new InputStreamReader(response.getEntity().getContent()));
            while((line = in.readLine()) != null) respTxt.append(line);
        } catch ( ClientProtocolException e){
            Log.e("ALFA", "HTTPReq:ClientProtocolException " + e.toString());
        } catch ( IOException e){
            Log.e("ALFA", "HTTPReq:IOException " + e.toString());
        } catch ( Exception e){
            Log.e("ALFA", "HTTPReq:Exception " + 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);
}
}

4

1 回答 1

0

进一步寻找问题的解决方案,我发现了使用套接字请求服务器的建议。我将Fiddler与我 PC 上的浏览​​器结合使用来检查发送到错误服务器和从错误服务器接收的数据,并阅读了 Wikipedia 上的一篇文章,解释了 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);
    }
}   
于 2013-06-24T21:50:39.693 回答