我的POST请求只响应源代码,而不是正确的XML响应

时间:2014-12-13 12:26:55

标签: html objective-c xml post

我正在尝试使用Echo 3.0 Web框架向网站发送帖子请求。我的目标是使用发布请求登录网站。我已经查看了使用firebug的请求,并且可以看到该网站需要以下列格式发布请求:

<cmsg xmlns="http://www.nextapp.com/products/echo/svrmsg/clientmessage.3.0" i="1">
<dir xmlns="" proc="CFocus">
<focus i="C.21"></focus>
</dir>
<dir xmlns="" proc="CSync">
<e t="action" i="C.21"></e>
<p i="C.19" n="selectionStart">5</p>
<p i="C.19" n="selectionEnd">5</p>
<p i="C.19" n="text">username</p>
<p i="C.20" n="selectionStart">10</p>
<p i="C.20" n="selectionEnd">10</p>
<p i="C.20" n="text">password</p>
</dir>
</cmsg>

Firebug也可以告诉我,我应该收到xml响应。问题是我收到了我已将请求发送到的页面的源代码。

这是我目前用来发送帖子请求的代码。

NSString *urlString = [NSString stringWithFormat:@"http://147.29.29.224"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

//set headers
NSString *contentType = [NSString stringWithFormat:@"text/xml; charset=UTF-8"];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

NSString *xmlString = @"<cmsg       xmlns='http://www.nextapp.com/products/echo/svrmsg/clientmessage.3.0' i='1'><dir xmlns='' proc='CFocus'><focus i='C.21'></focus></dir><dir xmlns='' proc='CSync'><e t='action' i='C.21'></e><p i='C.19' n='selectionStart'>5</p><p i='C.19' n='selectionEnd'>5</p><p i='C.19' n='text'>username</p><p i='C.20' n='selectionStart'>10</p><p i='C.20' n='selectionEnd'>10</p><p i='C.20' n='text'>password</p></dir></cmsg>";

[request setValue:[NSString stringWithFormat:@"%lu",
                   (unsigned long)[xmlString length]]
forHTTPHeaderField:@"Content-length"];

[request setHTTPBody:[xmlString
                      dataUsingEncoding:NSUTF8StringEncoding]];
//create the body

//get response
NSHTTPURLResponse* urlResponse = nil;
NSError *error = [[NSError alloc] init];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"Response Code: %ld", (long)[urlResponse statusCode]);
if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) {
    NSLog(@"Response: %@", result); 
    //here you get the response 
}

代码给我一个200的响应代码。

我做错了什么?我是否将帖子请求发送到了错误的页面?

//编辑:

我已经发现我已经将请求发送到稍微错误的地址,但我现在面临400响应代码的问题,尽管发送与通过firebug提交的请求完全相同的POST请求。

0 个答案:

没有答案