一个"/"引发的血案

来源:互联网 发布:虚拟币系统源码 编辑:程序博客网 时间:2024/05/16 00:50

问题背景:最进在解决在线升级应用与服务器端对接的问题,升级应用客户端中通过http的post方法向服务器端发送json数据,服务器端在接收到json数据后,将返回升级所需的数据以json格式下发给到客户端。服务器端采用apache+PHP5.6.31版本搭建。
问题现象:在进行对接的过程中发现服务器端没有收到客户端通过post提交的json数据,客户端代码如下:

    public HttpClient getHttpClient()    {    if( mHttpClient == null )    {        HttpParams params = new BasicHttpParams( );        HttpConnectionParams.setConnectionTimeout( params , HttpSessionConstant.CONNECTION_TIMEOUT );        HttpConnectionParams.setSoTimeout( params , HttpSessionConstant.SO_TIMEOUT );        HttpConnectionParams.setSocketBufferSize( params , HttpSessionConstant.SOCKET_BUFFER_SIZE );        HttpClientParams.setRedirecting( params , true );        mHttpClient = new DefaultHttpClient();        mHttpClient.getParams( ).setParameter( "http.useragent" , HttpSessionConstant.getUserAgent( ) );        Log.d(Tag,"not direct proxy here");        boolean useProxy = APNUtil.hasProxy( App.getAppContext( ) );        if( useProxy )        {            Log.d( Tag , "need getApnProxy = " + APNUtil.getApnProxy( App.getAppContext( ) ) );            Log.d( Tag , "need getApnPortInt = " + APNUtil.getApnPortInt( App.getAppContext( ) ) );            HttpHost proxy = new HttpHost( APNUtil.getApnProxy( App.getAppContext( ) ) , APNUtil.getApnPortInt( App                .getAppContext( ) ) );            mHttpClient.getParams( ).setParameter( ConnRoutePNames.DEFAULT_PROXY , proxy );}}
try {            HttpClient httpClient = getHttpClient( );            mHttpPost = new HttpPost( mUrl[mHasRetry] );            // 构造消息头            mHttpPost.setHeader("Content-type", "application/json; charset=utf-8");             if (null != mJsonParam) {                StringEntity entity = new StringEntity(mJsonParam.toString());                mHttpPost.setEntity(entity);                //LogUtils.e( "-------------------entity = " + mJsonParam.toString());            }            mResponse = httpClient.execute(mHttpPost);            int statusCode = mResponse.getStatusLine( ).getStatusCode( );            //LogUtils.e( "-------------------statusCode = " + statusCode);            if( statusCode == HttpStatus.SC_OK )            {                HttpEntity httpEntity = mResponse.getEntity( );                if( httpEntity != null )                {                    String byteData = EntityUtils.toString( httpEntity );                    //LogUtils.e( "-------------------byteData = " + byteData);                    if( mCallBack != null )                        mCallBack.onSucceed( byteData );                }                release( );                return;            }            String strStatus = mResponse.getStatusLine( ).toString( );            release( );            if( mCallBack != null )                mCallBack.onError( statusCode , strStatus );            continue;        }        catch ( Exception excp )        {           }        finally        {        }

服务器端PHP代码如下:(现学的PHP)

<?php    $params = file_get_contents('php://input','r');    echo $params;    if(!empty($params))    {        $obj=json_decode($params);        $action = $obj->action;        if ($action == "ReqUpdate")        {            $updateType = 0;            $oldRomVersion = $obj->RomVersion;            $oldRomType = $obj->RomType;            $newRomVersion = "20170728";            if ($oldRomVersion < $newRomVersion){                $updateType = 1;            }            $rspupdate_filename = "./rspupdate.json";            $rspupdate = json_decode(file_get_contents($rspupdate_filename,FILE_USE_INCLUDE_PATH));            $rspupdate->updateType = $updateType;            $rspupdate->oldRomType = $oldRomType;            $rspupdate->oldRomVersion = $oldRomVersion;            echo json_encode($rspupdate);        }        else if ($action == "ReqReported")        {            $rspreport_filename = "./rspreport.json";            $rspreport = json_decode(file_get_contents($rspreport_filename,FILE_USE_INCLUDE_PATH));            echo json_encode($rspreport);        }    }     else    {        echo file_get_contents("./rspreport.json",FILE_USE_INCLUDE_PATH);    }

升级服务器地址(搭建的apache 文件服务器)

 private static final String PUBLIC_OTA_URL_A = "http://192.168.1.106/allwinner_update";

每次执行升级操作时,服务器端返回的值都是“$params”为null,通过wireshark抓包发现,客户端通过post发送的json数据已经被服务器端收到,如下图:
这里写图片描述
如上图,服务器IP为192.168.1.106,客户端IP为192.168.1.118。服务器端收到了json数据,同时也对客户端下发了数据,但是为什么$params为null?
同时也注意到POST操作完成之后,客户端又做了一次处理301的重定向,会不会是这里引起的呢?

问题原因:过程就不再废话了,反正是浪费了半天的时间,才注意到301的问题原来是由于服务器地址引起的,由于客户端设置的服务器地址是“http://192.168.1.106/allwinner_update”,所以301的重定向又定位到“http://192.168.1.106/allwinner_update/”,原来就是一个“/”符号引起的。

谨记谨记,勿重蹈覆辙!

原创粉丝点击