通过 HTTPHeaderField 传递值时出现有趣的问题

Funny issue while passing a value by HTTPHeaderField

我正在尝试将一些带有空格和特殊字符的值从 Swift 程序传递到 php 脚本,方法是使用以下代码将它们归因于 HTTPHeaderFields:

let theRequest=NSMutableURLRequest(URL:NSURL(string:urlString)!, cachePolicy:.ReloadIgnoringLocalAndRemoteCacheData,
                    timeoutInterval:100.0)
theRequest.addValue(self.name, forHTTPHeaderField: "name")
theRequest.addValue(self.abbot, forHTTPHeaderField: "abbot")
theRequest.addValue(self.address, forHTTPHeaderField: "address")
theRequest.addValue(self.email!, forHTTPHeaderField: "email")
theRequest.addValue(self.phone!, forHTTPHeaderField: "phone")
theRequest.addValue(self.website!, forHTTPHeaderField: "website")

由 php 脚本接收:

$name=$_SERVER['HTTP_NAME'];
$abbot=$_SERVER['HTTP_ABBOT'];
$address=$_SERVER['HTTP_ADDRESS'];
$email=$_SERVER['HTTP_EMAIL'];
$phone=$_SERVER['HTTP_PHONE'];
$website=$_SERVER['HTTP_WEBSITE'];

此机制适用于所有变量,但适用于地址一。当我执行调用时出现错误:

PHP Notice: Undefined index: HTTP_ADDRESS in /var/www/html/iPhone/iPuja/insertMonastery.php on line 9

在php脚本中;在 HTTP_ADDRESS 是保留字的情况下,我尝试将地址名称更改为其他字符串,但无济于事。可能是什么问题?

谢谢,

问题是因为我没有对字符串进行 URLEncode。使用一次:

theRequest.addValue(self.address.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!, forHTTPHeaderField: "address")

值通过;出于某种原因,在发送带有特殊字符的非 urlencoded 字符串时没有给出错误,而是没有发送任何内容。 在 php 脚本上,我当然必须对它进行 urldecode 并设置:

$mysqli->query("SET NAMES utf8");

为了应对重音字符串和其他特殊字符。