gpt4 book ai didi

ios - 找不到与使用 ASP.NET for ios Swift 的请求 URI Web api 匹配的 HTTP 资源

转载 作者:行者123 更新时间:2023-11-29 05:57:12 25 4
gpt4 key购买 nike

我需要向服务器发送POST请求,这些是参数

let params : NSDictionary = ["userid":"\(userID)","sessionid":"\(sessionID)","site_id": "\(siteIDParam)", "latitude":"\(currentlatitude)", "longitude":"\(currentlongitude)", "action": "1", "comments":commentTextView.text ?? ""]

但问题是我需要将图像作为字节数组发送,并且我正在使用以下代码(Swift 3)

func uploadImageRequest(image: UIImage? , urlString: String, imageName : String ,param: [String: AnyObject]? , completion:@escaping(_ success:Bool , _ object : AnyObject?) -> ())
{
let url = NSURL(string: urlString)
let request = NSMutableURLRequest(url: url! as URL)
request.httpMethod = "POST"
let boundary = generateBoundaryString()
//define the multipart request type
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
if (image == nil)
{
return
}
let image_data = UIImageJPEGRepresentation(image!, 0.5)
if(image_data == nil)
{
return
}
let body = NSMutableData()
let fname = "photo.png"
let mimetype = "image/*"
body.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
body.append("Content-Disposition:form-data; name=\"\(imageName)\"; filename=\"\(fname)\"\r\n".data(using: String.Encoding.utf8)!)
body.append("Content-Type: \(mimetype)\r\n\r\n".data(using: String.Encoding.utf8)!)
body.append(image_data!)
body.append("\r\n".data(using: String.Encoding.utf8)!)
body.append("--\(boundary)--\r\n".data(using: String.Encoding.utf8)!)
if param != nil {
for (key, value) in param! {
body.append("--\(boundary)\r\n")
body.append("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
body.append("\(value)\r\n")
}
}
request.httpBody = body as Data
_ = URLSession.shared.dataTask(with: request as URLRequest) { (data, response, error) -> Void in
if let data = data {
// Json Response
let json = try? JSONSerialization.jsonObject(with: data, options: [])
// response.
if let response = response as? HTTPURLResponse , 200...299 ~= response.statusCode {
completion(true, json as AnyObject?)
} else {
completion(false, json as AnyObject?)
}
} else {
completion(false, error?.localizedDescription as AnyObject?)
}
}.resume()
}

我猜图像未正确转换,因此触发时响应失败,并且出现以下错误

Message = "No HTTP resource was found that matches the request URI 

该API在Android设备上工作正常,但在iOS设备上无法实现。我搜索了所有链接,但找不到合适的解决方案。我也尝试过使用 Alamofire 但仍然遇到同样的错误。

最佳答案

您必须在 .plist 文件中的 NSAppTransportSecurity dictionary 下将 NSAllowsArbitraryLoads 键设置为 YES

enter image description here

您可以使用 alamofire 轻松完成此操作,使用以下代码来完成这些操作。

 let multipartHeaders = ["Content-Type":"multipart/form-data"]
let paramString: String = "parameters in json string..."

Alamofire.upload(multipartFormData: { (multipartFormData) in
multipartFormData.append(paramString.data(using: .utf8)!, withName: "json")
multipartFormData.append(<imageDataHere>, withName: "myimage", fileName: "myImage.jpeg", mimeType: "image
jpeg")

}, usingThreshold: SessionManager.multipartFormDataEncodingMemoryThreshold, to: url, method: .post, headers:
multipartHeader) { (encodingResult) in
switch encodingResult {
case .success(let request, _, _):
request.responseJSON(completionHandler: { (response) in
print(response)
})
case .failure(let error):
print(error.localizedDescription)
}
}

关于ios - 找不到与使用 ASP.NET for ios Swift 的请求 URI Web api 匹配的 HTTP 资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55039438/

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