NSMutableURLRequest不更改HTTPMethod

时间:2015-01-28 16:33:38

标签: ios objective-c xcode nsmutableurlrequest

我在XCode中有以下方法:

- (void)makeRequest:(NSString *)wPin withMethod:(NSString *)HTTPMethod passValue:(NSString *)wVal {
    @try {

        NSString *params = @"";
        NSString *postString;
        NSData *postData;
        NSString *method;

        if ([HTTPMethod isEqualToString:@"GET"]) {
            params = [NSString stringWithFormat:@"?pin=%@", wPin];
            method = @"Retrieving data from";
        } else {
            postString = [NSString stringWithFormat:@"pin=%@&val=%@", wPin, wVal];
            postData = [postString dataUsingEncoding:NSUTF8StringEncoding];
            method = [NSString stringWithFormat:@"Passing '%@' to", postString];
        }

        NSString *requestURL = [NSString stringWithFormat:@"%@%@", homeURL, params];

        NSLog(@"%@ '%@'", method, requestURL);

        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:requestURL] cachePolicy:NSURLRequestUseProtocolCachePolicy  timeoutInterval:0];
        NSString *postLength = [[NSString alloc] initWithFormat:@"%lu", (unsigned long)[postData length]];

        [request setHTTPMethod:HTTPMethod];

        [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
        [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

        [request setHTTPBody:postData];

        NSString *postBody = [[NSString alloc] initWithBytes: [request.HTTPBody bytes] length: [request.HTTPBody length] encoding:NSUTF8StringEncoding];

        NSLog(@"%@", postBody);

        NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate: self];

        if (conn) { //Makes connection
        }
    }
    @catch (NSException *e) {
        NSLog(@"Exception %@", e);
    }

}

当我使用以下方法调用此方法时:

[self makeRequest:wPin withMethod:@"GET" passValue:@""];

我的服务器记录一个健康的GET请求,然后向我发送正确的数据,然后我解析它...它有效,没问题。

但是,当我使用:

调用此方法时
[self makeRequest:wPin withMethod:@"POST" passValue:wState];

我的服务器仍将请求方法记录为" GET"。我甚至尝试过直接设置HTTPMethod:

[request setHTTPMethod:@"POST"];

我的服务器仍然记录了一个" GET"请求。

仅供参考:我在JavaScript中开发了相同的应用程序,一切正常(服务器正确处理GET和POST)。

这是我的应用控制台输出:

(正在加载)

2015-01-28 10:25:08.324 Switch[2977:132599] Retrieving data from '<my server>?pin=18'
2015-01-28 10:25:08.330 Switch[2977:132599] Retrieving data from '<my server>?pin=23'
2015-01-28 10:25:08.611 Switch[2977:132599] 18OFF
2015-01-28 10:25:08.626 Switch[2977:132599] 23ON

(当我发送三个POST请求时)

2015-01-28 10:25:28.842 Switch[2977:132599] Passing 'pin=18&val=1' to '<my server>'
2015-01-28 10:25:28.843 Switch[2977:132599] pin=18&val=1
2015-01-28 10:28:12.189 Switch[2977:132599] Passing 'pin=18&val=0' to '<my server>'
2015-01-28 10:28:12.190 Switch[2977:132599] pin=18&val=0
2015-01-28 10:28:13.702 Switch[2977:132599] Passing 'pin=23&val=0' to '<my server>'
2015-01-28 10:28:13.703 Switch[2977:132599] pin=23&val=0

另外,当你看到&#34; pin = 18&amp; val = 1&#34;在输出中,请注意它在发送之前只是postBody的NSLog。这不是服务器响应,不像&#34; 18OFF&#34;你在输出中看到了。

2 个答案:

答案 0 :(得分:2)

尝试更改编码方案,

[postString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 

答案 1 :(得分:1)

问题在于DNS与我的服务器一起工作的方式。我必须绕过端口80,所以当我的域名指向它到达我的路由器的正确端口时,我会收到一个&#34; Object Moved&#34; POST请求的响应,除非我手动输入我的服务器的IP。这在某种程度上意味着POST请求在到达我的页面之前成为GET请求。在使用直接IP时,该应用程序工作。我必须在其他地方解决这个问题。感谢所有尝试过帮助的人!

(好消息是,NSMutableURLRequest有效!)