我试图在iOS 9中使用Swift 2 HTTP上传(到Windows IIS服务器)图像和表单参数,出于某种原因,即使我已经发布了两次发布帖子仅在按钮单击时调用该函数。
我已经检查过按钮没有多次触发。
我使用基本身份验证发布到HTTPS(我尝试过使用相同结果的HTTP)。它使用JustHttp(版本0.3)和Alamofire 3发生。
在Alamofire(和JustHTTP)中观察上传进度我可以看到它更新了totalBytesExpectedToWrite
,然后在某个时候通过上传,totalBytesExpectedToWrite
值翻倍。
我已经尝试过调试JustHTTP和Alamofire以了解为什么会发生这种情况,但无法找到它发生的位置。
我使用的JustHttp代码是:
Just.post(
"https://my.url.com",
auth:("xxxxxxxx", "xxxxxxxxx"),
timeout: 20,
data:["Name": tfName.text! as AnyObject,
"Email": tfEmail.text! as AnyObject,
files:[
"file":HTTPFile.Data("photo.jpg", imagedata!, nil)
],
asyncProgressHandler: {(p) in
print(p.type) // either .Upload or .Download
if (p.type == ProgressType.Upload)
{
dispatch_async(dispatch_get_main_queue()) {
// update some UI
self.textView.titleLabel.text = "Sending Entry...\(Int(p.percent*100))%"
}
}
},
asyncCompletionHandler: {(r) in
dispatch_async(dispatch_get_main_queue()) {
print(r.statusCode)
}
})
Alamofire代码是:
Alamofire.upload(Method.POST, "https://the.url.com", multipartFormData: { multipartFormData in
multipartFormData.appendBodyPart(data: imagedata!, name: "file", fileName: "photo.jpg", mimeType: "image/png")
multipartFormData.appendBodyPart(data: "My Name".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name: "Name")
multipartFormData.appendBodyPart(data: "My Email address".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name: "Email")
},
encodingCompletion: { encodingResult in
print("encoding done")
switch encodingResult {
case .Success(let upload, _, _):
upload.authenticate(user: "xxxxxx", password: "xxxxxxxxxx")
.progress { bytesWritten, totalBytesWritten, totalBytesExpectedToWrite in
print("a: \(bytesWritten) b: \(totalBytesWritten) c: \(totalBytesExpectedToWrite)")
}
upload.responseString { request, response, responseBody in
print(request)
print(response!.statusCode)
switch responseBody {
case Result.Success(let responseValue):
print(responseValue)
case Result.Failure(_, let error as NSError):
print(error)
default: break
}
}
case .Failure(let encodingError):
print(encodingError)
}
})
更新:
我现在在没有身份验证的情况下尝试使用Alamofire,它按预期工作,即只发送一次整个请求。
答案 0 :(得分:2)
好的,我已经解决了这个问题。除了auth参数之外,我还需要添加以下标题(在Just和Alamofire中)。我从Alamofire文档中获取了代码,但没有意识到我需要添加标题并包含auth属性。
let user = "xxxxxxx"
let password = "xxxxxxx"
let credentialData = "\(user):\(password)".dataUsingEncoding(NSUTF8StringEncoding)!
let base64Credentials = credentialData.base64EncodedStringWithOptions([])
let headers = ["Authorization": "Basic \(base64Credentials)"]
然后是Alamofire:
Alamofire.upload(Method.POST, "https://my.url.com", headers: headers, multipartFormData: [...]
JustHttp:
Just.post("https://my.url.com",
auth:(user, password),
headers: headers,
[...]
为了完整起见,远程Web服务器是运行Asp.Net C#MVC 5的IIS 7.5,带有以下Auth操作过滤器:
public class BasicAuthenticationAttribute : ActionFilterAttribute
{
public string BasicRealm { get; set; }
protected string Username { get; set; }
protected string Password { get; set; }
public BasicAuthenticationAttribute(string username, string password)
{
this.Username = username;
this.Password = password;
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var req = filterContext.HttpContext.Request;
var auth = req.Headers["Authorization"];
if (!String.IsNullOrEmpty(auth))
{
var cred = System.Text.ASCIIEncoding.ASCII.GetString(Convert.FromBase64String(auth.Substring(6))).Split(':');
var user = new { Name = cred[0], Pass = cred[1] };
if (user.Name == Username && user.Pass == Password) return;
}
var res = filterContext.HttpContext.Response;
res.StatusCode = 401;
res.AddHeader("WWW-Authenticate", String.Format("Basic realm=\"{0}\"", BasicRealm ?? "content.imaginecruising.co.uk"));
res.End();
}
}
C#MVC用法:
[BasicAuthenticationAttribute("myusername", "mypassword", BasicRealm = "my.domain.com")]
public ActionResult MyAction()
{