gpt4 book ai didi

iphone - 更改 NSURLConnection 的 userAgent

转载 作者:行者123 更新时间:2023-12-03 16:00:21 26 4
gpt4 key购买 nike

嘿,我正在使用 NSURL 连接来接收数据。

[NSURLConnection sendSynchronousRequest:
//create request from url
[NSURLRequest requestWithURL:
//create url from string
[NSURL URLWithString:url]
]
//request parameters
returningResponse:nil error:nil
]

是否可以更改用户代理字符串?现在是:

应用程序名称/应用程序版本 CFNetwork/459 Darwin/10.0.0.d3

最佳答案

对象-C:

NSString* userAgent = @"My Cool User Agent";
NSURL* url = [NSURL URLWithString:@"http://whatsmyuseragent.com/"];
NSMutableURLRequest* request = [[[NSMutableURLRequest alloc] initWithURL:url]
autorelease];
[request setValue:userAgent forHTTPHeaderField:@"User-Agent"];

NSURLResponse* response = nil;
NSError* error = nil;
NSData* data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];

swift :

let userAgent = "My Cool User Agent"
if let url = NSURL(string: "http://whatsmyuseragent.com/") {
let request = NSMutableURLRequest(URL: url)
request.setValue(userAgent, forHTTPHeaderField: "User-Agent")
var response:NSURLResponse? = nil;
var error:NSError? = nil;
if let data = NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: &error) {
// do something with your data
}
}

关于iphone - 更改 NSURLConnection 的 userAgent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1532206/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com