可能重复:
如果数据开始加载但不是webViewDidFinishLoad,则不会触发NSMutableURLRequest超时
我正在使用Async HTTP请求并将我的NSMutableURLRequest的超时设置为30秒,我想在此时间内超时请求.
问题:请求没有超时30秒.超时始终需要90秒.任何解决方案.
这是我的连接代码:
NSMutableURLRequest *myRequest = [[NSMutableURLRequest alloc] initWithURL:myURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30];
[myRequest setHTTPMethod:@"POST"];
[myRequest setHTTPBody:myData];
NSURLConnection *myConnection = [[NSURLConnection alloc] initWithRequest:myRequest delegate:self];
Run Code Online (Sandbox Code Playgroud)
我正在实施以下门格:
- (void)connection:(NSURLConnection *)iConnection didReceiveResponse:(NSURLResponse *)iResponse;
- (void)connection:(NSURLConnection *)iConnection didReceiveData:(NSData *)iElementContructionContextData;
- (void)connection:(NSURLConnection *)iConnection didFailWithError:(NSError *)iError;
Run Code Online (Sandbox Code Playgroud) 我有以下代码,它在iOS7中工作正常,但在iOS8中没有.在iOS8中,应用程序总是使用缓存,即使我已设置NSURLRequestReloadIgnoringCacheData:
NSURL *url = [NSURL URLWithString:k_Chapter_URL];
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url];
NSURLSessionConfiguration *sessionConfig =
[NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfig.requestCachePolicy = NSURLRequestReloadIgnoringCacheData;
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig];
[[session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
}] resume];
Run Code Online (Sandbox Code Playgroud)
有没有人和我有类似的情况?
我正在尝试对YouTube Data API v3进行授权调用,如下所述:https: //developers.google.com/youtube/v3/docs/activities/list
我得到了一切正常,直到我到达需要访问令牌的部分.
我添加了GoogleID登录,它为我提供了具有以下范围的访问令牌:
"https://www.googleapis.com/auth/youtube"
"https://www.googleapis.com/auth/youtube.force-ssl"
"https://www.googleapis.com/auth/youtube.readonly"
Run Code Online (Sandbox Code Playgroud)
这些是三个范围,在谷歌的参考中提到并使用.
let url = NSURL(string: theURLString)
let sessionConfig = NSURLSessionConfiguration.defaultSessionConfiguration()
let sessionQueue = NSOperationQueue.mainQueue()
let session = NSURLSession(configuration: sessionConfig, delegate: self, delegateQueue: sessionQueue)
let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "GET"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
if Settings.sharedInstance.isLoggedIn {
request.setValue("Bearer \(Settings.sharedInstance.userToken!)", forHTTPHeaderField: "Authorization")
}
let task = session.downloadTaskWithRequest(request)
task.resume()
Run Code Online (Sandbox Code Playgroud)
电话结束后,我得到了以下答案:
{
error = {
code = 401;
errors = (
{
domain = global;
location = Authorization; …Run Code Online (Sandbox Code Playgroud) 我们使用XML/NSMutableURLRequest从我们的网站上提取内容,有时它会通过"卷曲"样式的撇号和引号,而不是'.NSMutableURLRequest似乎讨厌这些并将它们变成奇怪的\ U00e2\U0080\U0099字符串.
有什么我可以做的事情来防止这种情况吗?我正在使用GET方法,所以我应该以某种方式告诉它使用UTF-8?或者,我错过了什么?
UIApplication* app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = YES;
NSString *urlStr = [NSString stringWithFormat:@"%@",url];
NSURL *serviceUrl = [NSURL URLWithString:urlStr];
NSMutableURLRequest *serviceRequest = [NSMutableURLRequest requestWithURL:serviceUrl];
[serviceRequest setHTTPMethod:@"GET"];
NSURLResponse *serviceResponse;
NSError *serviceError;
app.networkActivityIndicatorVisible = NO;
return [NSURLConnection sendSynchronousRequest:serviceRequest returningResponse:&serviceResponse error:&serviceError];
Run Code Online (Sandbox Code Playgroud) 我想将用户名和密码发布到服务器.我想将此用户名和密码转换为Base64编码.因此,将为Authorization字段添加此编码字符串.
是否有任何API可用于在iOS中进行Base64编码,或者我们自己编写?
如何将键值对添加到NSMutableURLRequest的主体?我知道我可以使用这样的东西:
NSURL *url = [[NSURL alloc] initWithString:@"http://localhost:8080/upload/"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:@"POST"];
NSString *bodyContents = @"fileName=";
[bodyContents stringByAppendingString:fileName];
[bodyContents stringByAppendingString:@"&"];
[bodyContents stringByAppendingString:@"deviceName="];
[bodyContents stringByAppendingString:deviceName];
[bodyContents stringByAppendingString:@"fileContents="];
[bodyContents stringByAppendingString:fileContents];
Run Code Online (Sandbox Code Playgroud)
但这似乎很麻烦,特别是如果我到达我需要在请求中提交大量值的地方.我希望苹果能提供类似[request addBodyField:@"fileName":fileName]或同样有用的方法,但我无法在文档中找到.有没有我遗失或者我只是坚持附加字符串?
我知道这可能有几个问题,但是当我尝试为我的UIWebView设置一个新的"用户代理"时,我遇到了一个问题.这就是我现在正在做的事情:
NSURL *myUrl = [NSURL URLWithString:auxUrl];
NSMutableURLRequest *myRequest = [NSMutableURLRequest requestWithURL:myUrl];
NSMutableString *auxUserAgent = [[NSMutableString alloc] initWithString:[myRequest valueForHTTPHeaderField: @"User-Agent"]];
[auxUserAgent appendString:@"(iOS)"];
[myRequest setValue:auxUserAgent forHTTPHeaderField:@"User-Agent"];
NSLog(@"UA : %@",[myRequest valueForHTTPHeaderField: @"User-Agent"]);
Run Code Online (Sandbox Code Playgroud)
我试图将一些文本附加到实际的用户代理,并在XCode控制台中显示新的用户代理值.
2013-12-16 19:03:42.200应用程序[736:60b] UA:Mozilla/5.0(iPhone; CPU iPhone OS 7_0_4,如Mac OS X)AppleWebKit/537.51.1(KHTML,如Gecko)Mobile/11B554a(iOS)
但是,当我运行我的应用程序时,它会显示旧的应用程序.以下是我在UIWebView中显示的网页中显示的用户代理的屏幕截图:

所以..我的问题是:我错过了什么?系统中有什么东西不允许开发人员更改这些数据?
谢谢.
我有一个简单的GET请求,我试图在我的macOS应用程序中运行.但是我一直收到以下错误:
找不到具有指定主机名的服务器.
我试图从中下载JSON数据的URL是:
https://suggestqueries.google.com/complete/search?client=safari&q=mercedes
如果我在浏览器或在线API测试人员网站(例如Hurl.it)中测试它,请求工作正常.然后自动下载JSON文件.
但是,通过我的macOS应用程序运行请求不起作用.这是我的代码:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://suggestqueries.google.com/complete/search?client=safari&q=mercedes"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setHTTPMethod:@"GET"];
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
[[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSLog(@"Data: %@", data);
NSLog(@"Response: %@", response);
NSLog(@"Error: %@", error);
}] resume];
Run Code Online (Sandbox Code Playgroud)
这是完整的错误日志:
dnssd_clientstub ConnectToServer:connect() - >尝试次数:1 dnssd_clientstub ConnectToServer:connect() - >尝试次数:2 dnssd_clientstub ConnectToServer:connect() - >尝试次数:3 dnssd_clientstub ConnectToServer:connect()失败
路径:/ var/run/mDNSResponder套接字:6错误:-1错误:1操作不被允许2017-10-27 09:58:31.610493 + 0100搜索建议[] nw_resolver_create_dns_service_locked DNSServiceCreateDelegateConnection失败:ServiceNotRunning(-65563)TIC TCP Conn失败[1:0x600000164080]:10:-72000 …
json objective-c nsmutableurlrequest nsurlsession nsurlsessiondatatask
当我尝试请求此URL时,我无法获取HTML源代码:http://www.google.co.in/search? q = search
NSURL *url = [NSURL URLWithString:@"http://www.google.co.in/search?q=search"];
NSMutableURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSData *data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:nil error:nil];
NSLog(@"Webdata : %@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
Run Code Online (Sandbox Code Playgroud)
数据始终为NULL.
您好,请帮助我如何为以下api准备NSMutableURLRequest
网址:www.XXXXXXXX.com/api.php
登录:
www.XXXXXXXXXXXX.com/api.php?task=login
发布数据 :-
“ email” =>用户的电子邮件
“ pw” =>用户密码
json响应:成功登录后的会话ID
我正在尝试这样。
NSMutableURLRequest *request;
request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"www.XXXXXXXX.com/api.php?task=login"]];
[request setHTTPMethod:@"POST"];
NSString *postString =@"email=xxxxxxxx@gmail.com&pw=1234";
[request setValue:[NSString
stringWithFormat:@"%d", [postString length]] forHTTPHeaderField:@"Content-length"];
[request setHTTPBody:[postString
dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *connection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
Run Code Online (Sandbox Code Playgroud) ios ×7
iphone ×4
objective-c ×4
nsurlsession ×2
base64 ×1
cocoa-touch ×1
google-api ×1
ios8 ×1
ipad ×1
json ×1
nsurl ×1
swift ×1
uiwebview ×1
user-agent ×1
xml ×1
youtube ×1