ios - NSMutableURLRequest 不改变 HTTPMethod

标签 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

此外,当您在输出中看到类似“pin=18&val=1”的内容时,请注意,这只是 postBody 发送前的一个 NSLog。这不是服务器响应,与您在输出中看到的“18OFF”不同。

最佳答案

尝试改变编码方案,

[postString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 

关于ios - NSMutableURLRequest 不改变 HTTPMethod,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28197480/

相关文章:

iphone - 使 UIView 全屏显示的问题

ios - 如何分解涉及移位运算符的表达式? swift

iphone - 如何在 iOS 上使用 NSLog 打印字符串常量

objective-c - 将 NSFont 存储到文档中

ios - Google Translation API 中的注音字符

iphone - 将部署 iOS 从 5.0 更改为 4.0 - 现在无法在我的 iPhone 上运行,但可以在模拟器上运行

objective-c - 在邮件 ViewController 中禁用横向模式

swift - 我需要帮助调试此错误 - child 数学测验

ios - 从多个 View 模型创建 View

ios - 没有找到适合核心数据迁移的映射模型