iphone – 更改NSURLConnection的userAgent

iphone – 更改NSURLConnection的userAgent,第1张

概述嘿,我使用NSURL连接接收数据。 [NSURLConnection sendSynchronousRequest://create request from url[NSURLRequest requestWithURL: //create url from string [NSURL URLWithString:url]] //request parametersreturn 嘿,我使用NSURL连接接收数据。

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

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

Appname / AppVersion CFNetwork / 459 Darwin / 10.0.0.d3

解决方法 Obj-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];

迅速:

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全部内容,希望文章能够帮你解决iphone – 更改NSURLConnection的userAgent所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1047543.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存