从 Objective c 到 asmx Web 服务的参数为空白
我正在从我的 xcode 调试器调用 .Net asmx Web 服务。在调试器中,我可以看到我正在传递一些值,但在 .net Web 服务上它是空白的。
在网络服务端,我正在写入日志文件。我看到该方法正在被调用,但参数为空/空。我已经粘贴了下面的代码。我也粘贴了wsdl。参数作为空白
-(void) fetchData: (NSString*)userid
{
NSString *requestSoapMsg =
[NSString stringWithFormat:
@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<soap:Envelope xmlns:xsi="
"\"http://www.w3.org/2001/XMLSchema-instance\" "
"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
"xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
"<soap:Body>\n"
"<UpdateRegForUser xmlns=\"http://soft.com\">\n"
"<userid>%@</userid>\n"
"</UpdateRegForUser>\n"
"</soap:Body>\n"
"</soap:Envelope>\n", userid];
NSLog(@"%@", requestSoapMsg); // i can see the value here
NSURL *url = [NSURL URLWithString:@"http://soft.com:8000/services/registration.asmx"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSString *requestSoapMsgLength = [NSString stringWithFormat:@"%d", [requestSoapMsg length]];
[request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"http://soft.com/UpdateRegForUser" forHTTPHeaderField:@"SOAPAction"];
[request addValue:requestSoapMsgLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[requestSoapMsg dataUsingEncoding:NSUTF8StringEncoding]];
NSError *error;
NSURLResponse *response;
NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if(!result)
NSLog(@"FAILEDDDDDDDDD!!");
WSDL 出现的任何原因 -
POST /services/registration.asmx HTTP/1.1
Host: soft.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://soft.com/UpdateRegForUser"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<UpdateRegForUser xmlns="http://soft.com/">
<userid>string</userid>
</UpdateRegForUser>
</soap:Body>
</soap:Envelope>
I am calling an .Net asmx web service from my xcode debugger. In the debugger I can see that I am passing some value but on the .net web service it is coming as blank.
On the web service end I am writing to a log file. I see that the method is getting called but the parameter is coming blank/null. I have pasted the code below. I have also pasted the wsdl. Any reason why the parameters are coming as blank
-(void) fetchData: (NSString*)userid
{
NSString *requestSoapMsg =
[NSString stringWithFormat:
@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<soap:Envelope xmlns:xsi="
"\"http://www.w3.org/2001/XMLSchema-instance\" "
"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
"xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
"<soap:Body>\n"
"<UpdateRegForUser xmlns=\"http://soft.com\">\n"
"<userid>%@</userid>\n"
"</UpdateRegForUser>\n"
"</soap:Body>\n"
"</soap:Envelope>\n", userid];
NSLog(@"%@", requestSoapMsg); // i can see the value here
NSURL *url = [NSURL URLWithString:@"http://soft.com:8000/services/registration.asmx"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSString *requestSoapMsgLength = [NSString stringWithFormat:@"%d", [requestSoapMsg length]];
[request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"http://soft.com/UpdateRegForUser" forHTTPHeaderField:@"SOAPAction"];
[request addValue:requestSoapMsgLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[requestSoapMsg dataUsingEncoding:NSUTF8StringEncoding]];
NSError *error;
NSURLResponse *response;
NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if(!result)
NSLog(@"FAILEDDDDDDDDD!!");
WSDL -
POST /services/registration.asmx HTTP/1.1
Host: soft.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://soft.com/UpdateRegForUser"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<UpdateRegForUser xmlns="http://soft.com/">
<userid>string</userid>
</UpdateRegForUser>
</soap:Body>
</soap:Envelope>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想到了问题所在。
我编写了一个 C# 控制台应用程序并调用了 Web 服务。同时,我捕获了 fiddler 的痕迹。 fiddler 跟踪显示 xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 应该出现在 xsi 之前。我保存了 xml 并使我的请求 xml 完全相同。它开始工作了。
I figured the problem.
I wrote a C# console application and invoked the web service. At the same time, I captured the trace from fiddler. The fiddler trace showed me that xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" should come before xsi. I saved the xml and made my request xml EXACTLY the same. It started working.