-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEZHttpClient.m
More file actions
88 lines (69 loc) · 2.7 KB
/
EZHttpClient.m
File metadata and controls
88 lines (69 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//
// EZHttpClient.m
//
// Created by Mike Brockey on 9/1/08.
//
/*
// Example Usage
EZHttpClient *client = [[[EZHttpClient alloc]init]autorelease];
// <form method="post">
// <input type="hidden" name="projectName" value="CCNet" />
// <input type="hidden" name="serverName" value="local" />
// <font color="Black">
// <input type="submit" name="ForceBuild" value="Force"/>
// <input type="submit" name="StopBuild" value="Stop"/>
// </font>
// </form>
NSArray *bodyKeys = [NSArray arrayWithObjects:@"projectName", @"serverName", @"ForceBuild", nil];
NSArray *bodyValues = [NSArray arrayWithObjects:project.name, @"local", @"Force", nil];
NSDictionary *bodyDictionary = [NSDictionary dictionaryWithObjects:bodyValues forKeys:bodyKeys];
[client makeRequest:url HttpMethod:@"POST"
HTTPHeadersFromDictionary:headerDictionary
HttpBodyFromDictionary:bodyDictionary];
*/
#import "EZHttpClient.h"
@implementation EZHttpClient
-(NSString*)makeRequest:(NSURL*)url HttpMethod:(NSString*)httpMethod HTTPHeadersFromDictionary:(NSDictionary*) httpHeaders
HttpBodyFromDictionary:(NSDictionary*)bodyValues
{
NSMutableURLRequest *post = [[[NSMutableURLRequest alloc] initWithURL:url] autorelease];
[post setHTTPMethod: httpMethod];
for (id key in httpHeaders)
{
[post addValue:[httpHeaders objectForKey:key] forHTTPHeaderField:key];
}
NSString* bodyBuilder = @"";
if(bodyValues != nil)
{
for (id key in bodyValues)
{
bodyBuilder = [bodyBuilder stringByAppendingFormat:@"&%@=%@", key, [bodyValues objectForKey:key]];
}
bodyBuilder = [bodyBuilder substringFromIndex:1];
}
[post setHTTPBody: [[[NSString alloc] initWithString:bodyBuilder]
dataUsingEncoding: NSASCIIStringEncoding]];
NSURLResponse* response;
NSError* error;
NSData* result;
result = [NSURLConnection sendSynchronousRequest:post returningResponse:&response error:&error];
return [[[NSString alloc] initWithData:result encoding:NSASCIIStringEncoding] autorelease];
}
-(NSString*)makeRequest:(NSURL*)url HttpMethod:(NSString*)httpMethod HTTPHeadersFromDictionary:(NSDictionary*) httpHeaders
HttpBodyFromString:(NSString*)body
{
NSMutableURLRequest *post = [[[NSMutableURLRequest alloc] initWithURL:url] autorelease];
[post setHTTPMethod: httpMethod];
for (id key in httpHeaders)
{
[post addValue:[httpHeaders objectForKey:key] forHTTPHeaderField:key];
}
[post setHTTPBody: [[[NSString alloc] initWithString:body]
dataUsingEncoding: NSASCIIStringEncoding]];
NSURLResponse* response;
NSError* error;
NSData* result;
result = [NSURLConnection sendSynchronousRequest:post returningResponse:&response error:&error];
return [[[NSString alloc] initWithData:result encoding:NSASCIIStringEncoding] autorelease];
}
@end