从iphone到wcf服务的POST请求?

时间:2013-01-08 06:03:24

标签: objective-c wcf http-post

我正在做从iPhone发送(POST)XML请求到wcf服务的概念,我知道发送XML请求的现象。但是我有从hcf服务检索到的html数据,我必须POST相同的数据作为服务请求。检索html数据后,我将其存储在NSString变量中,并在请求中传递该字符串。但是我无法POST并收到错误。 '反序列化EService.svc类型的对象时出错。令牌'[CDATA ['是预期但发现'DOCTYPE'。为什么我收到此错误?

来自服务的Html响应是这样的:

(
        {
        BodyEmailforResult = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
\n<html xmlns=\"http://www.w3.org/1999/xhtml\">
\n<head>
\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />
\n<title>Untitled Document</title>
\n</head>
\n
\n<body style=\"background:#bdd9ef; padding:20px 0px; margin:0px;\">
\n\t<table width=\"700px\" cellspacing=\"0\" cellpadding=\"0\" style=\"border-radius:10px; margin:0px auto; background:white; color:#2222; padding:20px 0px;\" >
\n
\n    <tr>
\n    \t<td style=\"height:130px; background:#e8e8e8; border-bottom:2px solid #c5c5c5; margin:0px; \" >
\n        \t<table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\">
\n            \t<tr>
\n                \t<td width=\"40%\" style=\"padding-left:20px;\" >
\n\t\t        \t\t<img src=\"http://123.32.34.45/images/logo.png\"  />
\n                    </td>
\n                    <td>
\n                    \t<p style=\"color:#1b1b1b; margin:0px; font-size:14px; font-family:Arial, Helvetica, sans-serif; line-height:20px; text-align:right; padding-right:20px; \" >Visit  website</p>
\n                        <p style=\"color:#0459df; margin:0px; font-size:14px; font-family:Arial, Helvetica, sans-serif; line-height:20px; text-align:right; padding-right:20px; text-decoration:underline; \"><a href=\"http://123.32.34.45\" style=\"color:#0459df; font-weight:bold; \" >123.32.34.45</a></p>
\n                    </td>
\n               </tr>
\n        \t</table>
\n        </td>
\n        
\n    </tr>
\n    <tr>
\n    \t<td height=\"20px\" >
\n        
\n        </td>
\n    </tr>
\n\t<tr>
\n    \t<td style=\"color:#1b1b1b; font-size:14px; text-align:left; font-family:Arial, Helvetica, sans-serif; line-height:24px; padding-left:20px; padding-right:20px; \" >
\n        \t<p style=\"margin:0px; line-height:30px; \" >Dear <span style=\"font-weight:bold;\" >Mary</span></p>
\n            
\n            
\n            <p style=\"margin:0px; line-height:24px; padding-top:10px; \" >
\n            \tThis is to confirm that we have received your registration request for your Account. We have created a temporary link listed below. You can use this link to activate your user account. 
\n            </p>
\n                        
\n            <p style=\"margin:0px; padding-top:10px; \" >You can <a href=\"123.32.34.45/Verify.aspx?Code=N6zDZK \" style=\"color:#0459df; font-weight:bold;\" >click hear </a> to activate your user account. </p>
\n            
\n            <p style=\"margin:0px; padding-top:10px;\" >Once you confirm your e-mail account, away to make. Note that use your e-mail and password to sign-in.
\n
\n</p>
\n<p style=\"margin:0px; font-size:14px; font-weight:bold; padding-top:10px;\" >Registration Details :</p>
\n<p style=\"margin:0px; padding-top:10px;\" >Your registration successfully completed Thanks for your registration</p>
\n
\n
\n<p style=\"margin:0px; font-size:16px; padding-top:30px;\" >
\nThanks & Regards
\n</p>
\n<p style=\"color:#0080b8; margin:0px; font-size:16px; font-weight:bold; padding-top:10px;\" >
\n
\n</p>
\n
\n
\n        </td>
\n    </tr>
\n    </table>
\n    
\n</body>
\n</html>";
    }
)

I took this response in string as :
 NSString *bodyofemail=[[arr2 objectAtIndex:0]objectForKey:@"BodyEmailforResult"];

and POST request is like this :

    NSString *url5=[NSString stringWithFormat:@"http://123.32.34.45/EService.svc/SendEmail"];
        NSMutableURLRequest *request5=[[[NSMutableURLRequest alloc] init]autorelease];
        [request5 setURL:[NSURL URLWithString:url5]];
        [request5 setHTTPMethod:@"POST"];
        NSString *contentType5=[NSString stringWithFormat:@"text/xml"];
        [request5 addValue:contentType5 forHTTPHeaderField:@"Content-Type"];
        NSMutableData *postBody5=[NSMutableData data];
        [postBody5 appendData:[[NSString stringWithFormat:@"<EmailServiceRequest xmlns=\"http://schemas.datacontract.org/2004/07/\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">"]dataUsingEncoding:NSUTF8StringEncoding]];
        [postBody5 appendData:[[NSString stringWithFormat:@"<Body>%@</Body>",bodyofemail]dataUsingEncoding:NSUTF8StringEncoding]];
        [postBody5 appendData:[[NSString stringWithFormat:@"<FromEmail>glory@i.net</FromEmail>"]dataUsingEncoding:NSUTF8StringEncoding]];
        [postBody5 appendData:[[NSString stringWithFormat:@"<Subject>%@</Subject>",subjectofemail]dataUsingEncoding:NSUTF8StringEncoding]];
        [postBody5 appendData:[[NSString stringWithFormat:@"<ToEmail>%@</ToEmail>",txtemail.text]dataUsingEncoding:NSUTF8StringEncoding]];
        [postBody5 appendData:[[NSString stringWithFormat:@"</EmailServiceRequest>>"]dataUsingEncoding:NSUTF8StringEncoding]];

    [request5 setHTTPBody:postBody5];
    NSHTTPURLResponse *urlResponse5=nil;
    NSError *error5=nil;
    NSData *responseData5 = [NSURLConnection sendSynchronousRequest:request5
                                                  returningResponse:&urlResponse5
                                                              error:&error5];

    if (responseData5!= NULL)
    {
        NSString *rss5 = [[NSString alloc] initWithData:responseData5
                                               encoding:NSUTF8StringEncoding ];
        NSLog(@"Response Code:%d",[urlResponse5 statusCode]);
        if([urlResponse5 statusCode ]>=200 && [urlResponse5 statusCode]<300)
        {
            NSLog(@"Response:%@",rss5);
        }
    }
    else
    {
        NSLog(@"Failed to send request: %@", [error5 localizedDescription]);
    }

但是我得到了这样的错误:'反序列化EService.svc类型的对象时出错。令牌'[CDATA ['是预期的,但找到'DOCTYPE'。

我哪里出错了以及如何将html数据发布到wcf服务?

任何帮助都会被指定..

1 个答案:

答案 0 :(得分:0)

WCF服务通常要求请求为soap个请求,除非该服务已配置为使用允许XML或JSON请求的WebHttpBinding。您应该从服务开发人员那里了解该服务是仅接受soap XML还是接受纯XML或JSON。

在任何一种情况下,解决此类问题的最简单方法是从成功调用(来自另一个服务客户端)捕获XML请求(soap或不捕获),并将这些消息用作XML(或JSON)的模板)你需要在iOS应用程序中构建字符串。