标签: nsurlrequest

NSURLConnection,NSURLRequest,不受信任的证书和用户身份验证

大家早上好,

我一直在尝试编写一个从需要身份验证的远程Web服务执行某些GET的应用程序.我的主要问题是这些远程服务器中的大多数(并且有很多远程服务器)没有有效的证书.我有代码接受无效的证书和代码,以正确的uname&pass(下面)来响应挑战.我遇到的问题是让两人一起玩.我似乎无法找到一种方法来发送挑战NSURLCredential或正确链接回调的方法.当我试图将它们连接起来时,我无法NSURLRequest接到didReceiveAuthenticationChallenge两次电话.

任何想法将不胜感激!

认证代码......

-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{   
    if(!hasCanceled){
        if ([challenge previousFailureCount] == 0) {
            NSURLCredential *newCredential;
            newCredential=[NSURLCredential credentialWithUser:_username password:_password persistence:NSURLCredentialPersistenceNone];
            [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
        } 
        else {
            [[challenge sender] cancelAuthenticationChallenge:challenge];
            NSLog(@"Bad Username Or Password");
            badUsernameAndPassword = YES;
            finished = YES;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

iphone cocoa nsurlconnection nsurlrequest nsurlcredential

8
推荐指数
1
解决办法
3821
查看次数

使用NSURL请求时网址错误

我试图在iPhone 4.0 SDK中请求这种URL(访问令牌略有修改,因为你真的不需要看到它):

https://graph.facebook.com/me?sdk=ios&sdk_version=2&access_token=123902817987|8cb9e8408d2685cef853cd80.9-747882379|UGu5NvcAHiXuGEGzkq&format=json&limit=40&until=1286619821
Run Code Online (Sandbox Code Playgroud)

但是我得到了这样的信息:

Failed with error: Error Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0x9e657a0 {NSUnderlyingError=0x9e656a0 "bad URL", NSLocalizedDescription=bad URL}
Run Code Online (Sandbox Code Playgroud)

当我在Safari或Chrome中复制并粘贴时,它可以正常工作.我试着按照这里的建议进行更换|,但不起作用.%|

从终端询问它看起来像这样:

curl https://graph.facebook.com/me/statuses?sdk=ios&sdk_version=2&access_token=9999955817987|8ab9e8408d2685cef.3-747882379|UGuxWDuM&format=json&limit=40&until=1286619821
[1] 16190
[2] 16191
[3] 16194
[4] 16195
[5] 16196
[2]   Done                    sdk_version=2
[4]-  Done                    format=json
$ -bash: UGu5NvcAHiXuGEGzkq3KP8xWDuM: command not found
-bash: 8ab9e8408d2685cef853cd80.3-747882379: command not found
{"error":{"type":"OAuthException","message":"An active access token must be used to query information about the current user."}}
[1]   Done                    curl https://graph.facebook.com/me/statuses?sdk=ios
[3]-  Exit 127                access_token=9999955817987 | 8ab9e8408d2685cef.3-747882379 | …
Run Code Online (Sandbox Code Playgroud)

iphone objective-c nsurlrequest ios4

8
推荐指数
2
解决办法
2万
查看次数

NSURLRequest:如何将httpMethod"GET"更改为"POST"

GET方法,它工作正常.

网址:http://myurl.com/test.html?query = id = 123& name = kkk


我没有POST方法的概念.请帮我.

我如何将GET方法转换为POST方法?


网址:http://testurl.com/test.html

[urlRequest setHTTPMethod:@"POST"];

iphone post nsurlrequest ios

8
推荐指数
2
解决办法
2万
查看次数

iOS Twitter NSURLErrorDomain代码= -1012

我试图通过在我的应用程序中使用twitter阅读来获取用户的联系方式.我在github上发现这个项目看起来真的很棒.我只遇到一个问题.如果我使用来自Twitter的新应用消费者密钥和消费者密钥代码运行它会给我一个错误:

Error Domain=NSURLErrorDomain Code=-1012 "The operation couldn’t be completed. (NSURLErrorDomain error -1012.)" UserInfo=0x6898e80 {NSErrorFailingURLKey=https://api.twitter.com/oauth/request_token, NSErrorFailingURLStringKey=https://api.twitter.com/oauth/request_token, NSUnderlyingError=0x68980e0 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1012.)"}
Run Code Online (Sandbox Code Playgroud)

控制台中的输出是:

2013-01-16 17:55:24.367 DMTwitterOAuth[3411:c07] current status = Prompt for user data and request token to server
2013-01-16 17:55:24.371 DMTwitterOAuth[3411:c07] current status = Requesting token for current user's auth data...
2013-01-16 17:55:25.433 DMTwitterOAuth[3411:c07] current status = Token received from server
Run Code Online (Sandbox Code Playgroud)

在我得到警报视图后,它立即说它遇到了一个NSURLErrorDomain.

另一方面,如果我使用来自另一个Twitter应用程序的其他代码运行它,它会工作,向我提供有关用户的所有详细信息.我只是无法理解为什么它可以使用某些键,而对于其他键则不然.

twitter nsurlrequest ios nsurlerrordomain

8
推荐指数
1
解决办法
1万
查看次数

Swift 3:URLSession/URLRequest不起作用

我仍然试图将我们的应用程序从Swift 2转换为Swift 3,因为我被迫,因为我们所有的Apple设备现在都在运行iOS 10.

我已经完成了代码转换,并认为我做得很好,但在尝试调试我的JSON问题时(在另一个问题中发布),我现在处理的请求甚至没有被发送.

let params: [String:AnyObject] = [
    "email":"\(self.preferences.string(forKey: "preference_email")!)" as AnyObject
]
let requestParams: [String:AnyObject] = [
    "action":"601" as AnyObject,
    "params":params as AnyObject
]

do {
    let requestObject = try JSONSerialization.data(withJSONObject: requestParams, options:[])
    var request = URLRequest(url: URL(string: "http://domain.tld/path/")!)

    request.httpBody = requestObject
    request.httpMethod = "POST"

    let config = URLSessionConfiguration.default
    let session = URLSession(configuration: config)

    NSLog("Got here?")

    session.dataTask(with: request) {data, response, error in
        guard let data = data, error == nil else {
            print("error=\(error)")
            return
        }

        NSLog("Got here …
Run Code Online (Sandbox Code Playgroud)

nsurlrequest ios nsurlsession swift swift3

8
推荐指数
1
解决办法
1万
查看次数

如何删除NSMutableURLRequest中的标头?

如何发出不包含标题字段的请求?请求从头开始发送到我自己的服务器实现,这不关心头字段.该请求最多只包含一个帖子正文.如果我错过了一些合乎逻辑的东西,请告诉我.

请不要告诉我有关ASIHTTPRequest的信息.谢谢.

iphone objective-c nsurlrequest nsmutableurlrequest ios

7
推荐指数
2
解决办法
4980
查看次数

nsurlconnection异步请求

首先,问题是失败的问题..如果你只是想看看它们是什么,请跳到这篇文章的底部,你会看到它们以粗体显示..更多细节,那么你可以阅读这篇文章的其余部分......

我只是试图解决我的NSURLConnection,以便它的工作顺利,我理解这一点.互联网上的异步连接缺乏示例/教程,或者没有任何我能找到的解释说明除了获得连接和运行之外的任何深度,除了工作之后,它似乎非常简单.希望这个问题可以填补我觉得其他用户的空白.

因此,在我的.h文件中,我导入了基础头并声明了接收或缺少接收数据所需的方法(错误等).

.H

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h> //add foundations
//.. other headers can be imported here

@interface MyViewController: UITableViewController { 

//Im not setting any delegates to access the methods because Is all happening in the same 
//place so I just use the key word 'self' when accessing the methods declared below
//I'm not sure if this is the best thing to do but I wasn't able to get my head around declaring the delegate or how it would help …
Run Code Online (Sandbox Code Playgroud)

iphone nsurlconnection nsurlrequest nsurlcache ios

7
推荐指数
1
解决办法
1万
查看次数

使用SOAP Web服务时出现"未找到Action标头"错误消息

在iOS App中使用SOAP Web服务时出现以下错误

"No Action header was found with namespace 'http://www.w3.org/2005/08/addressing' for the given message."
Run Code Online (Sandbox Code Playgroud)

同样的Web服务在SOAP UI工具中运行良好.

以下是请求格式

NSString *data = @"<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:tem=\"http://tempuri.org/\">
<soap:Header></soap:Header>
<soap:Body><tem:GetEvaluators></tem:GetEvaluators></soap:Body>
</soap:Envelope>";

NSString *url = @"webservice url";
NSData *postData = [data dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
[request setTimeoutInterval:20.0];
[request setValue:@"application/soap+xml;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"http://tempuri.org/IATCService/GetEvaluators" forHTTPHeaderField:@"SOAPAction"];
 NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
Run Code Online (Sandbox Code Playgroud)

从webservice收到完整的错误响应

<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing"> …
Run Code Online (Sandbox Code Playgroud)

soap wsdl objective-c nsurlrequest ios

7
推荐指数
1
解决办法
8722
查看次数

NSURL在解开Optional值时发现nil

任何人都知道我为什么会这样

致命错误:在展开Optional值时意外发现nil

当我使用

    let URL = NSURL(string: "https://roads.googleapis.com/v1/snapToRoads?path=-35.27801,149.12958|-35.28032,149.12907")!
Run Code Online (Sandbox Code Playgroud)

nsurl nsurlrequest swift

7
推荐指数
2
解决办法
6358
查看次数

未设置NSURLSessionConfiguration HTTPAdditionalHeaders

已设置授权标头NSURLSessionConfiguration,但未附加NSURLSessionDataTask.这是一个错误Foundation framework吗?

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
[configuration setHTTPAdditionalHeaders:@{@"Authorization":@"123"}];

// Initialize session with NSURLSessionConfiguration
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
NSURLSessionDataTask *sessionTask = [session dataTaskWithRequest:request
                                        completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
}];

[sessionTask resume];
Run Code Online (Sandbox Code Playgroud)

foundation nsurlrequest ios nsurlsession nsurlsessionconfiguration

6
推荐指数
2
解决办法
7989
查看次数