gpt4 book ai didi

php - Android 上的 idHTTP Not Acceptable 错误 406

转载 作者:太空宇宙 更新时间:2023-11-03 13:46:46 25 4
gpt4 key购买 nike

我正在尝试使用 idHTTP 和 PHP 脚本在 MySQL 数据库上发布插入内容。这是要插入数据库的 PHP 脚本:

    $mysqli = new mysqli($servidor, $usuario, $senha, $banco);

// Caso algo tenha dado errado, exibe uma mensagem de erro
if (mysqli_connect_errno()) trigger_error(mysqli_connect_error());

$iduser = quoted_printable_decode($_POST['iduser']);
$nome = quoted_printable_decode($_POST['nome']);
$data = quoted_printable_decode($_POST['data']);
$hora = quoted_printable_decode($_POST['hora']);
$mensagem = quoted_printable_decode($_POST['mensagem']);
$latitude = quoted_printable_decode($_POST['latitude']);
$longitude = quoted_printable_decode($_POST['longitude']);
$imagem = $_FILES["imagem"]['tmp_name'];
$tamanho = $_FILES['imagem']['size'];

header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
header('Content-Type: text/plain; charset="utf-8"');

if ( $imagem != "none" )
{
$fp = fopen($imagem, "rb");
$conteudo = fread($fp, $tamanho);
$conteudo = addslashes($conteudo);
fclose($fp);

$queryInsercao = "INSERT INTO tabpainel (iduser, nome, data, hora, mensagem, latitude, longitude, imagem) VALUES ('$iduser', '$nome', '$data','$hora','$mensagem', '$latitude', '$longitude', '$conteudo')";

mysqli_query($mysqli,$queryInsercao) or die("Algo deu errado ao inserir o registro. Tente novamente.");

if (mysqli_affected_rows($mysqli) > 0)
include 'baixarpainel.php';
else
print utf8_encode("Não foi possível inserir o registro");
}
else
print utf8_encode("Não foi possível carregar a imagem.");
?>

在 Delphi 中,我使用的是:

      FormPHP := TIdMultiPartFormDataStream.Create;

FormPHP.AddFile ('imagem', AImagem, 'image/jpeg');
FormPHP.AddFormField ('iduser', AIDUser, 'utf-8');
FormPHP.AddFormField ('nome', ANome, 'utf-8');
FormPHP.AddFormField ('data', AData, 'utf-8');
FormPHP.AddFormField ('hora', AHora, 'utf-8');
FormPHP.AddFormField ('mensagem', AMensagem, 'utf-8');
FormPHP.AddFormField ('latitude', '1');
FormPHP.AddFormField ('longitude', '1');

Response := TStringStream.Create('',TEncoding.UTF8);

HTTP:= TIdHTTP.Create(self);
HTTP.Post('http://addressexample.com/cadastro.php',FormPHP,Response);

在不得不更换托管公司之前,它一直运行良好。使用 Hostinger 还可以,但使用 Hostgator 则不行。使用 Hostgator,idHTTP 在 EIdHTTPProtocalException 类中引发异常,消息为:“HTTP/1.1 406 Not Acceptable”。 Hostgator 支持已经禁用了可能导致问题的 mod_security

此异常仅发生在 Android 上。在 Windows 上使用相同的应用程序,它工作正常。

更新:我尝试了另一件事。 PHP 脚本是这样的:

    // Conecta-se ao banco de dados MySQL
$mysqli = new mysqli($servidor, $usuario, $senha, $banco);

// Caso algo tenha dado errado, exibe uma mensagem de erro
if (mysqli_connect_errno()) trigger_error(mysqli_connect_error());

# Instanciando o XMLWriter
$xml = new XMLWriter;
$xml->openMemory();

# Definindo o encoding do XML
$xml->startDocument( '1.0', 'UTF-8');

# Primeiro elemento do XML
$xml->startElement("DATAPACKET");
$xml->writeAttribute("version", "2.0");
$xml->StartElement("METADATA");
$xml->startElement("FIELDS");
$xml->startElement("FIELD");
$xml->writeAttribute("attrname", "id");
$xml->writeAttribute("fieldtype", "I4");
$xml->endElement();
$xml->startElement("FIELD");
$xml->writeAttribute("attrname", "iduser");
$xml->writeAttribute("fieldtype", "String");
$xml->writeAttribute("Width", "30");
$xml->endElement();
$xml->startElement("FIELD");
$xml->writeAttribute("attrname", "nome");
$xml->writeAttribute("fieldtype", "String");
$xml->writeAttribute("Width", "200");
$xml->endElement();
$xml->startElement("FIELD");
$xml->writeAttribute("attrname", "data");
$xml->writeAttribute("fieldtype", "String");
$xml->writeAttribute("Width", "8");
$xml->endElement();
$xml->startElement("FIELD");
$xml->writeAttribute("attrname", "hora");
$xml->writeAttribute("fieldtype", "String");
$xml->writeAttribute("Width", "5");
$xml->endElement();
$xml->startElement("FIELD");
$xml->writeAttribute("attrname", "mensagem");
$xml->writeAttribute("fieldtype", "String");
$xml->writeAttribute("Width", "3000");
$xml->endElement();
$xml->startElement("FIELD");
$xml->writeAttribute("attrname", "latitude");
$xml->writeAttribute("fieldtype", "r8");
$xml->endElement();
$xml->startElement("FIELD");
$xml->writeAttribute("attrname", "longitude");
$xml->writeAttribute("fieldtype", "r8");
$xml->endElement();
$xml->startElement("FIELD");
$xml->writeAttribute("attrname", "imagem");
$xml->writeAttribute("fieldtype", "bin.hex");
$xml->writeAttribute("subtype", "Binary");
$xml->endElement();
$xml->endElement(); //FIELDS
$xml->endElement(); //METADATA

$xml->StartElement("ROWDATA");
# Query na tabela escolhida
$rs_table = $mysqli->query("select * from tabpainel ORDER BY id DESC LIMIT 50");
while($table = $rs_table->fetch_array(MYSQLI_ASSOC))
{
# Transformando array em objeto
$table = (object)$table;
# Criando elemento tabela
$xml->StartElement("ROW");
# Setando os atributos
$xml->writeAttribute("id", "$table->id");
$xml->writeAttribute("iduser", "$table->iduser");
$xml->writeAttribute("nome", "$table->nome");
$xml->writeAttribute("data", "$table->data");
$xml->writeAttribute("hora", "$table->hora");
$xml->writeAttribute("mensagem", "$table->mensagem");
$xml->writeAttribute("latitude", "$table->latitude");
$xml->writeAttribute("longitude","$table->longitude");
$xml->writeAttribute("imagem", base64_encode("$table->imagem"));
$xml->endElement();
}
# Fechando o ROWDATA
$xml->endElement();
# Fechando o elemento DATAPACKET
$xml->endElement();
# Encerrando a conexao
//$con->close();
# Definindo cabecalho de saida
header("content-type: application/xml; charset=utf-8");
# Imprimindo a saida do XML
print $xml->outputMemory(true);
?>

我使用 http.get 来接收 xml:

Http.HandleRedirects:= true;
Http.request.useragent := 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; MAAU)';
MS.Text:= Http.get('http://addressexample.com/baixarpainel.php');
MS.SaveToFile(FarquivoBaixado);

这在 Android 上也能正常工作。问题仍然只存在于 Android 上的 http.post。

最佳答案

TIdHTTP 在所有平台上的工作方式完全相同,因为 Indy 使用单一的跨平台代码库。所以生成的 HTTP 请求在所有平台上应该是完全一样的。

HTTP 406 错误发生在 HTTP 请求包含 Accept header 时,该 header 未指定服务器能够在其中呈现响应的任何媒体类型。每 RFC 2616 Section 14.1 :

If no Accept header field is present, then it is assumed that the client accepts all media types. If an Accept header field is present, and if the server cannot send a response which is acceptable according to the combined Accept field value, then the server SHOULD send a 406 (not acceptable) response.

您的 PHP 脚本正在发送一个 text/plain 响应,因此如果您发送一个不允许 text/plainAccept header ,那么这可能会导致 406 错误。听起来 Hostgator 比 Hostinger 执行得更多。

默认情况下,TIdHTTP 将其 Request.Accept 属性设置为以下字符串值:

'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'

这在技术上允许所有媒体类型通过 */*,但优先级低于其他一些媒体类型。但是,如果服务器正确实现了 Accept 处理,那么该默认值仍然足以允许 text/plain 响应。

您需要联系 Hostgator 并与他们讨论问题,因为问题出在他们,而不是您。

也就是说,由于您知道服务器响应始终是 text/plain,因此您可以在调用 Post() 之前将以下内容添加到您的代码中:

HTTP.Request.Accept := 'text/plain';
HTTP.Request.AcceptCharset := 'utf-8';

关于php - Android 上的 idHTTP Not Acceptable 错误 406,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40851649/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com