繁体   English   中英

java-http请求不适用于长文本

[英]java-http request doesnt work for long text

我从互联网上获取了代码,并尝试通过http post发送大文本。 我不确定该代码是否用于http发布,但我相信。 我有一个php页面,我在其中获取参数并将其添加到数据库中。 当文本为5 6个字符时,该方法可以正常工作并更新数据库。 当我发送类似300个字符的消息时,它不会更新数据库。 我在数据库中的消息列是longtext类型。 可能是什么问题? 提前致谢。

 public String sendMessage(String  username, String  tousername, String message) throws UnsupportedEncodingException 
{           
    String params = "username="+ URLEncoder.encode(this.username,"UTF-8") +
                    "&password="+ URLEncoder.encode(this.password,"UTF-8") +
                    "&to=" + URLEncoder.encode(tousername,"UTF-8") +
                    "&message="+ URLEncoder.encode(message,"UTF-8") +
                    "&action="  + URLEncoder.encode("sendMessage","UTF-8")+
                    "&";        
    Log.i("PARAMS", params);
    return socketOperator.sendHttpRequest(params);      
}

 public String sendHttpRequest(String params)
{       
    URL url;
    String result = new String();
    try 
    {
        url = new URL(AUTHENTICATION_SERVER_ADDRESS);
        HttpURLConnection connection;
        connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);

        PrintWriter out = new PrintWriter(connection.getOutputStream());

        out.println(params);
        out.flush();
        out.close();

        BufferedReader in = new BufferedReader(
                new InputStreamReader(
                        connection.getInputStream()));
        String inputLine;

        while ((inputLine = in.readLine()) != null) {
            result = result.concat(inputLine);              
        }
        in.close();         
    } 
    catch (MalformedURLException e) {
        e.printStackTrace();
    } 
    catch (IOException e) {
        e.printStackTrace();
    }           

    if (result.length() == 0) {
        result = HTTP_REQUEST_FAILED;
    }

    return result;


}

PHP代码

case "sendMessage":
if ($userId = authenticateUser($db, $username, $password)) 
    {   
    if (isset($_REQUEST['to']))
    {
         $tousername = $_REQUEST['to']; 
         $message = $_REQUEST['message'];   

         $sqlto = "select Id from  users where username = '".$tousername."' limit 1";



                if ($resultto = $db->query($sqlto))         
                {
                    while ($rowto = $db->fetchObject($resultto))
                    {
                        $uto = $rowto->Id;
                    }
                    $sql22 = "INSERT INTO `messages` (`fromuid`, `touid`, `sentdt`,     `messagetext`) VALUES ('".$userId."', '".$uto."', '".DATE("Y-m-d H:i")."', '".$message."');";                       

                            error_log("$sql22", 3 , "error_log");
                        if ($db->query($sql22)) 
                        {
                                $out = SUCCESSFUL;
                        }               
                        else {
                                $out = FAILED;
                        }                       
                    $resultto = NULL;
                }   

    $sqlto = NULL;
    }
    }
    else
    {
        $out = FAILED;
    }   
break;

echo out;



   class MySQL
{   
    private $dbLink;
    private $dbHost;
    private $dbUsername;
        private $dbPassword;
    private $dbName;
    public  $queryCount;



    function MySQL($dbHost,$dbUsername,$dbPassword,$dbName)
        {
            $this->dbHost = $dbHost;
            $this->dbUsername = $dbUsername;
            $this->dbPassword = $dbPassword;
            $this->dbName = $dbName;    
            $this->queryCount = 0;      
        }
        function __destruct()
        {
            $this->close();
        }
        //connect to database
        private function connect() {    
            $this->dbLink = mysql_connect($this->dbHost, $this->dbUsername, $this->dbPassword);     
            if (!$this->dbLink) {           
                $this->ShowError();
                return false;
            }
            else if (!mysql_select_db($this->dbName,$this->dbLink)) {
                $this->ShowError();
                return false;
            }
            else {
                mysql_query("set names latin5",$this->dbLink);
                return true;
            }
            unset ($this->dbHost, $this->dbUsername, $this->dbPassword, $this->dbName);     
        }   
        /*****************************
         * Method to close connection *
         *****************************/
        function close()
        {
            @mysql_close($this->dbLink);
        }
        /*******************************************
         * Checks for MySQL Errors
         * If error exists show it and return false
         * else return true  
         *******************************************/
        function ShowError()
        {
            $error = mysql_error();
            //echo $error;      
        }   
        /****************************
         * Method to run SQL queries
         ****************************/
        function  query($sql)
        {   
            if (!$this->dbLink) 
                $this->connect();

            if (! $result = mysql_query($sql,$this->dbLink)) {
                $this->ShowError();         
                return false;
            }
            $this->queryCount++;    
            return $result;
        }
        /************************
        * Method to fetch values*
        *************************/
        function fetchObject($result)
        {
            if (!$Object=mysql_fetch_object($result))
            {
                $this->ShowError();
                return false;
            }
            else
            {
                return $Object;
            }
        }
        /*************************
        * Method to number of rows
        **************************/
        function numRows($result)
        {
            if (false === ($num = mysql_num_rows($result))) {
                $this->ShowError();
                return -1;
            }
            return $num;        
        }
        /*******************************
         * Method to safely escape strings
         *********************************/
        function escapeString($string)
        {
            if (get_magic_quotes_gpc()) 
            {
                return $string;
            } 
            else 
            {
                $string = mysql_escape_string($string);
                return $string;
            }
        }

        function free($result)
        {
            if (mysql_free_result($result)) {
                $this->ShowError();
                return false;
            }   
            return true;
        }

        function lastInsertId()
        {
            return mysql_insert_id($this->dbLink);
        }

        function getUniqueField($sql)
        {
            $row = mysql_fetch_row($this->query($sql));

            return $row[0];
        }
        function testconnection() { 
            $this->dbLink = mysql_connect($this->dbHost, $this->dbUsername, $this->dbPassword);     
            if (!$this->dbLink) {           
                $this->ShowError();
                return false;
            }
            else if (!mysql_select_db($this->dbName,$this->dbLink)) {
                $this->ShowError();
                return false;
            }
            else {
                mysql_query("set names latin5",$this->dbLink);
                return true;
            }
            unset ($this->dbHost, $this->dbUsername, $this->dbPassword, $this->dbName);     
        }       
    }

问题不在于Java方面,而在于PHP方面。

当您在POST或GET中对字符串进行URLEncode时,服务器端将对URL编码进行解码,然后将结果放入其$ _REQUEST变量(以及$ _POST变量)中。

因此,URL编码不能防止您在文本中使用单引号。 为此,您需要在PHP中使用转义命令以使字符串安全地插入数据库。 具体命令取决于您的数据库。 请注意,在您提供的MySQL类中,有一个escapeString方法。 用那个

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM