gpt4 book ai didi

iphone - 具有多个参数的 NSURLRequest

转载 作者:行者123 更新时间:2023-11-28 22:54:02 26 4
gpt4 key购买 nike

我正在尝试设置一个 NSURLRequest 来下载一个简单的 index.html 及其 externa style.css 表,但我不太确定该怎么做。我只是将请求的 URL 格式化为我想要的文件.. 但这必须略有不同,我找不到一个很好的例子来说明我正在尝试做的事情。

到目前为止,这是我的代码:

#pragma mark - NSURLConnection methods

- (void)htmlRequest
{
// Create the request.
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.mywebsite.com/index.html"]
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:60.0];

// create the connection with the request
// and start loading the data
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
// Create the NSMutableData to hold the received data.
// receivedData is an instance variable declared elsewhere.
receivedData = [NSMutableData data];
} else {
// Inform the user that the connection failed.
NSLog(@"Connection Fail");
}
}


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// This method is called when the server has determined that it
// has enough information to create the NSURLResponse.

// It can be called multiple times, for example in the case of a
// redirect, so each time we reset the data.

// receivedData is an instance variable declared elsewhere.
[receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{

// Append the new data to receivedData.
// receivedData is an instance variable declared elsewhere.
[receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
// inform the developer of error type

}

// This method uses methodName to determin which Initalizer method to send the response data to in EngineResponses.m
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// EngineResponses *engineResponses = [EngineResponses sharedManager];

// [engineResponses GetManufacturers:receivedData];

NSString *myString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
NSLog(@"%@", myString);

}

如您所见,我只是直接调用 index.html。我想知道如何格式化我的请求,以便获得 index.html 和 style.css

如有任何帮助,我们将不胜感激。

最佳答案

我总是创建一个新的数据结构,它有一个 -connection 属性和一个 -request 属性,就像这样

@interface connectionWrapper : NSObject
@property(retain) NSURLRequest *request
@property(retain) NSURLConnection *connection

通过在可变数组中保留此数据结构,您可以通过迭代数组并将每个 connectionWrapper 实例的 -connection 属性与回调方法的 connection 参数进行比较来区分回调方法中的连接,如果它们匹配(指向同一个对象),那么您可以检索 connectionWrapper 实例的 -request 属性,然后是 NSURLRequest 实例的 -url 属性。

因为我的母语不是英语,所以我认为代码是更好的导师。

-(NSURLRequest*)getRequestByConnection:(NSURLConnection*)connection
{
for(connectionWrapper *w in theArrayContainingAllConnectionWrappers)
{
if(w == connection)
return w.request;
}
}

在回调方法中:

-(void)connection:(NSURLConnection*)connection didReceiveResponse(NSURLResponse*)response
{
NSURLRequest *request = [self getRequestByConnection:connection];
NSURL *url = [request url];
/*apply different approach to different url/*
}

PS:很遗憾 NSURLConnection 没有 -request 属性,因此我们可以轻松检索与连接关联的请求。

关于iphone - 具有多个参数的 NSURLRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11182564/

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