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]
The value for the header 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 = @"";
NSMutableURLRequest *faURLRequest =
[NSMutableURLRequest requestWithURL:[NSURL URLWithString:feedURLString]];

[faURLRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[faURLRequest setHTTPMethod:@"POST"]; 
[faURLRequest addValue:@"" forHTTPHeaderField:@"SOAPAction"];

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

NSString *soapMessage = [NSString stringWithFormat:
                         @"<s:Envelope xmlns:s=\"\">\n"
                         "<s:Body >\n"
                         "<GetChanges xmlns=\"\">\n"
                         "</s:Envelope>\n", localLastSync];
[faURLRequest setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

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