我在 returnData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:nil error:nil];
以下是我正在使用的代码
NSString* curl = @"https://Some Url?ticket=";
curl = [curl stringByAppendingString:self.ticket];
curl = [curl stringByAppendingString:@"&apikey=hjgajgfjaghjf&XMLString="];
curl = [curl stringByAppendingString:stringB];
curl = [curl stringByReplacingOccurrencesOfString:@"\n" withString:@""];
NSURL *finalURL = [NSURL URLWithString:curl];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:finalURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10];
[theRequest setHTTPMethod:@"POST"];
NSData* returnData= [NSURLConnection sendSynchronousRequest:theRequest returningResponse:nil error:nil];
Run Code Online (Sandbox Code Playgroud)
任何一个可以告诉我为什么我在得到泄漏returndata我relased的returndata并尝试过,但我仍得到.
谢谢
我将 Storyboard 与导航控制器和 prepareforsegue 一起使用。我有两个 UITableViews。如果您单击第一个表格中的一行,您将进入第二个表格。第二个表根据您在第一个表中单击的行从 plist 中选取其数据。只要有互联网连接,这就可以正常工作。如果没有互联网连接,它会崩溃。
现在我想在加载第二个表之前检查是否有互联网连接。如果没有互联网连接,我想显示一个 UIAlertView。
我想用 NSURLConnection 做到这一点,但我不知道在哪里实现代码。我会将它放在 prepareforsegue 的第一张桌子的 .m 中,还是放在第二张桌子的 .m 中?
谢谢。
我有一个应用程序,它首先作为 iPhone 应用程序存在,现在作为 Android 应用程序存在。其中一个功能是加载一个网页,该网页通过将字符串作为 Referer 传递来绕过安全登录。iPhone的代码如下 -
NSMutableURLRequest *request = [NSMutableURLRequest new];
[request setValue:@"http://myweb.com/" forHTTPHeaderField:@"referer"];
[request setURL:[NSURL URLWithString:@"http://www.theirweb.com/meetings/meetings.plx?CID=TEST2012&O=Generic&Key=4s6p2wz9"]];
[htmlDoc loadRequest:request];
Run Code Online (Sandbox Code Playgroud)
我在我的 Android 应用程序中使用的代码是 -
WebView webview = new WebView(this);
setContentView(webview);
webview.loadUrl("http://www.theirweb.com/meetings/meetings.plx?CID=TEST2012&O=Generic&Key=4s6p2wz9");
Run Code Online (Sandbox Code Playgroud)
但是,当我运行它时,访问被拒绝,因为我没有将请求作为“Referer”发送。我怎样才能在我的 Android 代码中做到这一点?
我正在通过互联网异步下载四个 plist 文件。我需要等到所有四个文件都下载完毕,直到我在第一次运行时推送 UIViewController 或在所有后续运行中刷新数据并重新加载我的所有 UITableViews。
在第一次运行时,一切正常。刷新时,所有四个 url 请求都被调用和启动,但从不调用它们的完成或失败块,并且 UI 冻结。这很奇怪,因为我在后台线程中执行所有操作。我一直无法弄清楚为什么会发生这种情况。
第一次加载和刷新方法以相同的方式调用四个“更新”方法,并以相同的方式使用 NSCondition。
对于第一次运行:
- (void)loadContentForProgram:(NSString *)programPath
{
NSLog(@"Start Load Program");
AppDelegate *myDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
hud = [[MBProgressHUD alloc] initWithView:myDelegate.window];
[myDelegate.window addSubview:hud];
hud.labelText = @"Loading...";
hud.detailsLabelText = @"Loading Data";
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
//Do stuff here to load data from files
//Update From online files
hud.detailsLabelText = @"Updating Live Data";
resultLock = NO;
progressLock = NO;
recallLock = NO;
stageLock = NO;
condition = [[NSCondition alloc] init];
[condition lock]; …Run Code Online (Sandbox Code Playgroud) 我想使用Web服务登录.
我的网站是基于https的.我正在使用以下教程.
http://agilewarrior.wordpress.com/2012/02/01/how-to-make-http-request-from-iphone-and-parse-json-result/
Run Code Online (Sandbox Code Playgroud)
以下代码工作正常.
responseData = [NSMutableData data];
NSURLRequest *request = [NSURLRequest requestWithURL:
[NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=500&types=food&name=harbour&sensor=false&key=AIzaSyAbgGH36jnyow0MbJNP4g6INkMXqgKFfHk"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
Run Code Online (Sandbox Code Playgroud)
但我的代码是
responseData = [NSMutableData data];
NSURLRequest *request = [NSURLRequest requestWithURL:
[NSURL URLWithString:@"https:myurl/login?userId=username&password=111111"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
Run Code Online (Sandbox Code Playgroud)
它给出了错误
Connection failed: Error Domain=NSURLErrorDomain Code=-1202 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be --My URL-- which could put your confidential information at risk." UserInfo=0xa294260 {NSErrorFailingURLStringKey=https://mywebsite/login?userId=username&password=111111, NSLocalizedRecoverySuggestion=Would you like to connect to the server …Run Code Online (Sandbox Code Playgroud) 每当我尝试向服务器发出请求时,我都会收到此错误.在我们设置SSL之前它工作正常,而不是全部搞砸了......我的代码是:
// Setup URL POST Request..
NSString* code4ArmourUrl = [NSString stringWithFormat:@"https://api.code4armour.com/index.php/user/login?"];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:code4ArmourUrl ]];
[request setHTTPMethod:@"POST"];
[request addValue:@"00000000000000000000000000000000" forHTTPHeaderField:@"X-api-key"];
NSString* postString = [NSString stringWithFormat:@"email=%@&password=%@",emailString,passwordString];
postString = [postString stringByReplacingOccurrencesOfString:@"+" withString:@"%2b"];
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
// Make URL POST Request..
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError* connectionError) {
Run Code Online (Sandbox Code Playgroud)
为什么这不起作用的任何原因..我相信这是SSL的一个问题,但我不知道怎么解决这个问题而不做一些hacky,苹果不会最终接受到App Store?
任何帮助将不胜感激!谢谢.
我需要使用HTTPBody of Array向我的服务器发送一个post请求.这是我的参数数组:
params = [
"message" : [
"alert" : "Find your iPhone",
"sound" : "Binocular_Default.caf"
]
]
Run Code Online (Sandbox Code Playgroud)
现在我需要将NSMutableURLRequest's' 设置HTTPBody为此数组.我怎样才能做到这一点?
我正在尝试根据此代码https://cloud.google.com/natural-language/reference/rest/v1/documents in Swift对 Google 的 Cloud Natural Language API 提出请求,但我不太明白语法对吗?
import Foundation
import SwiftyJSON
class GoogleNaturalLanguageParser {
let session = URLSession.shared
var googleAPIKey = "XXX"
var googleURL: URL {
return URL(string: "https://language.googleapis.com/v1/documents:analyzeEntities?key=\(googleAPIKey)")!
}
//TODO: Add document
private func createRequest(with text: String, handler: @escaping (String) -> Void) {
// Create our request URL
var request = URLRequest(url: googleURL)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue(Bundle.main.bundleIdentifier ?? "", forHTTPHeaderField: "X-Ios-Bundle-Identifier")
// Build our API request
let jsonRequest = [
"requests": [ …Run Code Online (Sandbox Code Playgroud) 尝试调用内部有一些特殊字符的url字符串请求时,我遇到了一些问题.例如,使用CharacterSet,当在URL中具有该%20平均空格键时,我使用addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)它替换为的函数%2520.你们可以试试这段代码:
let url = "http://analytics.abc.io/acct?start_date=2017-11-15&aff=ABC%20Telecom"
url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
print(URL(string: url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)!)
Run Code Online (Sandbox Code Playgroud)
实际上它是成功的请求,但响应数据是错误的.任何人都可以给我一些想法,以及如何解决这个问题.
除此之外,请给我一些关于我不使用它的URLQueryItem的评论,所以我需要你的帮助.谢天谢地!
我正在 webView 中加载 url,现在我想阻止 webView 中的 url 中的广告。我怎样才能实现这个目标?
[_webVw loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:page_url]]];
Run Code Online (Sandbox Code Playgroud) nsurlrequest ×10
ios ×6
objective-c ×3
swift ×3
xcode ×2
adblock ×1
android ×1
http-referer ×1
https ×1
ios6 ×1
iphone ×1
java ×1
memory-leaks ×1
nscondition ×1
ssl ×1
url ×1
wkwebview ×1