0

私はiPhoneからwcfサービスにXMLリクエストを送信(POST)するという概念を行っており、XMLリクエストを送信する現象を知っていました.しかし、wcfサービスから取得したhtmlデータがあり、リクエストと同じデータをPOSTする必要がありますservice.html データを取得した後、それを NSString 変数に格納し、その文字列を request.But に渡して 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 サービスに POST する方法は?

どんな助けでも歓迎されます..

4

1 に答える 1

0

XML または JSON 要求のいずれかを使用するようにサービスが構成されていない限り、WCF サービスでは通常、要求が SOAP 要求である必要があります。WebHttpBindingサービスが SOAP XML のみを受け入れるのか、それともプレーンな XML または JSON を受け入れることができるのかを、サービス開発者に確認する必要があります。

どちらの場合でも、この種の問題に取り組む最も簡単な方法は、(別のサービス クライアントからの) 成功した呼び出しから XML 要求 (soap であるかどうかに関係なく) をキャプチャし、それらのメッセージを XML (または JSON) 文字列のテンプレートとして使用することです。 iOS アプリに組み込む必要があります。

于 2013-01-08T14:01:41.927 に答える