Wednesday, May 1, 2013

NSMutableURLRequest addValue example ios


addValue :forHTTPHeaderField:

Adds an HTTP header to the receiver’s HTTP header dictionary.
- (void)addValue:(NSString *)value forHTTPHeaderField:(NSString *)field
Parameters of [NSMutableURLRequest addValue]
value
The value for the header field.
field
The name of the header field. In keeping with the HTTP RFC, HTTP header field names are case-insensitive
Discussion of [NSMutableURLRequest addValue]
This method provides the ability to add values to header fields incrementally. If a value was previously set for the specified field, the supplied value is appended to the existing value using the appropriate field delimiter. In the case of HTTP, this is a comma.
Example of [NSMutableURLRequest addValue]
[urlRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
Example of [NSMutableURLRequest addValue]
static NSString *feedURLString = @"http://www.mydomain.com/DownloadService.svc";
NSMutableURLRequest *faURLRequest =
[NSMutableURLRequest requestWithURL:[NSURL URLWithString:feedURLString]];

[faURLRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[faURLRequest setHTTPMethod:@"POST"]; 
[faURLRequest addValue:@"http://tempuri.org/IDownloadService/GetChanges" forHTTPHeaderField:@"SOAPAction"];

NSString *localLastSync;
if (userProfile.lastSync == nil)
{
    localLastSync = @"2011-09-01T12:00:00";
}
else
{
    localLastSync = userProfile.lastSync;
}

NSString *soapMessage = [NSString stringWithFormat:
                         @"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
                         "<s:Body >\n"
                         "<GetChanges xmlns=\"http://tempuri.org/\">\n"
                         "<lastSync>%@</lastSync>\n"
                         "</GetChanges>\n"
                         "</s:Body>\n"
                         "</s:Envelope>\n", localLastSync];
[faURLRequest setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

self.downloadConnection =
[[NSURLConnection alloc] initWithRequest:faURLRequest delegate:self];