我尝试在Objective-C中创建HTTP Get.
它使用NSMutableURLRequest *请求;在Objective-C中,使用[request setHTTPMethod:@“GET”];选择GET或POST.
并通过以下代码设置标头:
[request setValue:@"Application/json" forHTTPHeaderField:@"Accept"]; [request addValue:@"Application/json" forHTTPHeaderField:@"Content-Type"]; [request addValue:[NSString stringWithFormat:@"Basic %@",USER_PWD] forHTTPHeaderField:@"Authorization"];
我引用链接How to make an HTTP request in Swift?,它只显示以下代码:
let url = NSURL(string: "http://www.stackoverflow.com") let request = NSURLRequest(URL: url) NSURLConnection.sendAsynchronousRequest(request,queue: NSOperationQueue.mainQueue()) {(response,data,error) in println(NSString(data: data,encoding: NSUTF8StringEncoding)) }
但它没有选择Get或Post,也没有设置标题.
如何判断Http方法是Get还是Post?以及如何设置标题?在斯威夫特
提前致谢.
解决方法
request.HTTPMethod = "POST" request.addValue("application/json",forHTTPHeaderField: "Content-Type") request.addValue("application/json",forHTTPHeaderField: "Accept")