我正在尝试使用我的应用程序建立到服务器的HTTPS连接.但由于跟随错误,连接失败
错误域= NSURLErrorDomain代码= -1200"发生SSL错误,无法与服务器建立安全连接." UserInfo = 0x612eb30 {NSErrorFailingURLStringKey = https:myURL.com/signup,NSLocalizedRecoverySuggestion =您是否还要连接到服务器?,NSErrorFailingURLKey = https:myURL.com/signup,NSLocalizedDescription =发生了SSL错误并且安全连接到服务器无法生成.,NSUnderlyingError = 0x612eb70"发生SSL错误,无法建立与服务器的安全连接."}
连接到服务器的代码是
-(IBAction) handleEvents:(id)sender
{
if ((UIButton*)sender == submit) {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSLog(@"Begin");
NSData *urlData;
NSURLResponse *response;
NSError *error;
NSString *url =[[NSString alloc]initWithFormat:@"%@signup",baseURL];
NSURL *theURL =[NSURL URLWithString:url];
NSMutableURLRequest *theRequest =[NSMutableURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:0.0f];
[theRequest setHTTPMethod:@"POST"];
NSString *theBodyString = [NSString stringWithFormat:@"emailId=%@&mobileNumber=%@&appId=%@&password=%@&firstName=%@&lastName=%@"
,@"abc@example.com",@"919879876780",@"bf1c7a6b3d266a7fe350fcfc4dda275211c13c23" ,@"qwerty" , @"Dev" , @"Sri"];
NSData *theBodyData = [theBodyString dataUsingEncoding:NSUTF8StringEncoding];
[theRequest setHTTPBody:theBodyData];
urlData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];
}
}
Run Code Online (Sandbox Code Playgroud)
我的委托方法是
- …Run Code Online (Sandbox Code Playgroud) 我正在发布一个小图片,所以我希望超时间隔很短.如果图像在几秒钟内没有发送,它可能永远不会发送.由于某种未知的原因NSURLConnection,无论我设置多短,我都永远不会失败timeoutInterval.
// Create the URL request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
initWithURL:[NSURL URLWithString:@"http://www.tumblr.com/api/write"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:0.00000001];
/* Populate the request, this part works fine */
[NSURLConnection connectionWithRequest:request delegate:self];
Run Code Online (Sandbox Code Playgroud)
我有一个断点,- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error但它永远不会被触发.我的图像继续张贴得很好,尽管很小,它们仍然出现在Tumblr上timeoutInterval.
我正在编写一个与服务器连接的应用程序NSURLConnection.
在委托方法中didreceiveresponse,如果状态代码是404,我取消连接,我想显示一条消息,其中包含在服务器中生成的自定义错误.
问题是,从响应对象,我只能得到状态码,标题,mimetype等,但没有身体.
我如何从中获取身体信息NSURLResponse?
objective-c nsurlconnection nsurlrequest http-status-code-404
如果我从终端运行此请求,我可以正常看到JSON请求:
curl -XGET 192.168.0.6:8888/scripts/data/backend2/index.php/name/_all
Run Code Online (Sandbox Code Playgroud)
我的NSURlRequest代码是这样的:
NSURLRequest *request = [NSURLRequest requestWithURL:
[NSURL URLWithString:@"192.168.0.6:8888/scripts/data/backend2/index.php/name/_all"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
Run Code Online (Sandbox Code Playgroud)
我收到这个错误:
didFailWithError
2013-11-29 22:31:08.164 Ski Greece[607:a0b] Connection failed: Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo=0xcd042d0 {NSErrorFailingURLStringKey=192.168.0.6:8888/scripts/data/backend2/index.php/name/_all, NSErrorFailingURLKey=192.168.0.6:8888/scripts/data/backend2/index.php/name/_all, NSLocalizedDescription=unsupported URL, NSUnderlyingError=0xdbdcc70 "unsupported URL"}
Run Code Online (Sandbox Code Playgroud)
如何调用该URL?我无法访问服务器代码 - 我知道只是设置回报我需要的东西,如果我调用该URL?
我想知道如何仅从URL请求异步获取返回值1或0 ....
目前我是这样做的:
NSString *UTCString = [NSString stringWithFormat:@"http://web.blah.net/question/CheckQuestions?utc=%0.f",[lastUTCDate timeIntervalSince1970]];
NSLog(@"UTC String %@",UTCString);
NSURL *updateDataURL = [NSURL URLWithString:UTCString];
NSString *checkValue = [NSString stringWithContentsOfURL:updateDataURL encoding:NSASCIIStringEncoding error:Nil];
NSLog(@"check Value %@",checkValue);
Run Code Online (Sandbox Code Playgroud)
这是有效的,但是它阻止了我的主线程,直到我从URL得到回复,我该如何设置它以便它将在另一个线程而不是主线程中执行?
编辑:ANSWER 我最终用这个来调用我的功能,它运行良好:)
[self performSelectorInBackground:@selector(shouldCheckForUpdate) withObject:nil];
Run Code Online (Sandbox Code Playgroud) 我正在使用Xcode 6作为带有Swift的iOS应用程序.我有一个带有嵌入式UIWebView的简单ViewController.你可以在下面找到代码.现在我想更改User-AgentHTTP标头.我尝试使用该setValue方法,NSURLRequest但它不起作用(请参阅未注释的行).有谁知道这是怎么做到的吗?
import UIKit
class WebViewController: UIViewController {
@IBOutlet weak var webView: UIWebView!
override func viewDidAppear(animated: Bool) {
var url = NSURL(string: "https://www.samplepage.com")
var request = NSMutableURLRequest(URL: url)
// request.setValue("Custom-Agent", forHTTPHeaderField: "User-Agent")
webView.loadRequest(request)
}
}
Run Code Online (Sandbox Code Playgroud) 我想对我做一个POST请求,WKWebView但是当我用Charles监视请求时,标题没有设置,因此请求失败.这有什么不对?
NSString *post = [NSString stringWithFormat: @"email=%@&password=%@", email, password];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *contentLength = [NSString stringWithFormat:@"%d", postData.length];
NSURL *url = [NSURL URLWithString:@"http://materik.me/endpoint"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];
[request setValue:contentLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[webview loadRequest:request];
Run Code Online (Sandbox Code Playgroud)
这就是查尔斯所说的请求如下:
POST /endpoint HTTP/1.1
Host: materik.me
Content-Type: application/x-www-form-urlencoded
Origin: null
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (iPhone; CPU OS 8_0 like Mac OS X)
Content-Length: 0
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Run Code Online (Sandbox Code Playgroud)
因此,正如您所看到的那样, …
以下代码成功连接到我的Ruby on Rails API并使用AFNetworking返回JSON.我需要做什么来编辑它以传递用户名和密码,以便我的API可以使用HTTP基本身份验证?
我已经阅读了他们的文档,但我是Objective-C和AFNetworking的新手,目前还没有意义.
NSURL *url = [[NSURL alloc] initWithString:@"http://localhost:3000/tasks.json"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation
JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request
, NSHTTPURLResponse *response
, id JSON) {
self.tasks = [JSON objectForKey:@"results"];
[self.activityIndicatorView stopAnimating];
[self.tableView setHidden:NO];
[self.tableView reloadData];
NSLog(@"JSON");
} failure:^(NSURLRequest *request
, NSHTTPURLResponse *response
, NSError *error
, id JSON) {
NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
}];
[operation start];
Run Code Online (Sandbox Code Playgroud) 我最近在比较两个NSURL并将一个NSURL与NSString(这是一个URL地址)进行比较时遇到了问题,情况是我从某个地方得到了一个NSURLRequest,我可能知道也可能不知道它指向的URL地址,我有一个URL NSString,比如"http://m.google.com",现在我需要检查NSURLRequest中的URL是否与我的URL字符串相同:
[[request.URL.absoluteString lowercaseString] isEqualToString: [self.myAddress lowercaseString]];
Run Code Online (Sandbox Code Playgroud)
这返回NO,因为它absoluteString给了我"http://m.google.com/",而我的字符串是"http://m.google.com",最后没有斜线,即使我使用创建NSURLRequest
[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://m.google.com"]]
Run Code Online (Sandbox Code Playgroud)
它仍然给我"http://m.google.com/" absoluteString,我想有没有可靠的方法来比较NSURL或一个NSURL和一个NSString?
检查一个'包含'是否另一个,但这不可靠,因为'http://m.google.com/blabla'包含'http://m.google.com'.
将NSString转换为NSURL并使用该isEqual方法比较两个NSURL并希望NSURL的实现isEqual可以搞清楚吗?
基于步骤2,但使用standardizedURL?将每个NSURL转换为标准URL ?
非常感谢!
嗨,我有以下访问URL的代码:
NSString * stringURL = [NSString stringWithFormat:@"%@/%@/someAPI", kSERVICE_URL, kSERVICE_VERSION];
NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:stringURL]];
AFJSONRequestOperation * operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
completionHandler(JSON, nil);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
completionHandler(nil, error);
}];
Run Code Online (Sandbox Code Playgroud)
但我想将用户令牌作为参数传递HEADER,如X-USER-TOKEN.
无法找到它AFNetworking documentation,我应该更改操作类型吗?
nsurlrequest ×10
objective-c ×6
ios ×5
json ×3
afnetworking ×2
iphone ×2
uiwebview ×2
https ×1
nsstring ×1
nsurl ×1
ssl ×1
swift ×1
user-agent ×1
wkwebview ×1
xcode ×1