首先,我们使用下面的代码,因为api只适用于移动设备,而不适用于浏览器。 "
$iPod = stripos($_SERVER['HTTP_USER_AGENT'], "iPod");
$iPhone = stripos($_SERVER['HTTP_USER_AGENT'], "iPhone");
$iPad = stripos($_SERVER['HTTP_USER_AGENT'], "iPad");
$Android = stripos($_SERVER['HTTP_USER_AGENT'], "Android");
$json_data = json_decode(file_get_contents('php://input'));
if ($json_data) {
$username_1 = $json_data->username;
if ($Android || $iPad || $iPhone || $iPod) {}
其次,api正在使用AFNetworking在android(使用volley)和ios objective-c上工作。但是,现在我将项目移植到swift3,我正在使用" Alamofire"用于网络电话。
方法::(这是我发出POST请求的方式):
let headers : HTTPHeaders = [
"Accept" : "application/json"
]
Alamofire.request("API URL", method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers)
.responseJSON{
response in
if let json = response.result.value{
}
但是,我收到错误404,我们通常在浏览器中遇到api时会得到错误。 之后,我删除了后端检查,请求工作正常,得到了正确的响应。但是,我们无法删除该支票。所以请帮助我,如果我在其他方面的标题上犯了任何错误。
这是我在debugPrint(response)::
中得到的{ status code: 200, headers {
"Access-Control-Allow-Origin" = "*";
"Cache-Control" = "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";
Connection = "Keep-Alive";
"Content-Encoding" = gzip;
"Content-Length" = 84;
"Content-Type" = "text/html; charset=UTF-8";
Date = "Thu, 14 Sep 2017 17:03:17 GMT";
Expires = "Thu, 19 Nov 1981 08:52:00 GMT";
"Keep-Alive" = "timeout=5";
Pragma = "no-cache";
Server = "Apache/2.4.25";
Vary = "Accept-Encoding,User-Agent";
"X-Powered-By" = "PHP/5.6.31";
} }
[Data]: 80 bytes
[Result]: SUCCESS: {
"is_valid" = 0;
status = 404;
"status_message" = "access denied";
success = 0;
}
答案 0 :(得分:0)
Alomafire发送默认的标头参数User-Agent,但其值类似于此iOS Example/1.0 (com.alamofire.iOS-Example; build:1; iOS 10.0.0) Alamofire/4.0.0
。
您显然可以提供自己的自定义标题:
let headers: HTTPHeaders = [
"User-Agent": "iPhone",
"Accept": "application/json"
]
Alamofire.request("<Ypur URL>", headers: headers)
.responseJSON { response in
debugPrint(response)
}
或者,如果您想保留其他默认标头参数
var headers = Alamofire.SessionManager.defaultHTTPHeaders
headers["User-Agent"] = "iPhone"