[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所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)