我一直在关注教程http://iphonebyradix.blogspot.ca/2011/04/working-with-webservices.html并决定使用xaramin创建自己的网络服务并尝试调用它。但它似乎并没有因某种原因而被召唤。
Service1.asmx
[WebMethod]
public string printsomeThing(string userData){
Console.WriteLine("test");
Console.Out.WriteLine();
return userData;
}
POST /Service1.asmx
SOAPAction: http://tempuri.org/printsomeThing
Content-Type: text/xml; charset=utf-8
Content-Length: string
Host: string
<?xml version="1.0" encoding="utf-16"?>
<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>
<printsomeThing xmlns="http://tempuri.org/">
<userData>string</userData>
</printsomeThing>
</soap:Body>
</soap:Envelope>
HTTP/1.0 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: string
<?xml version="1.0" encoding="utf-16"?>
<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>
<printsomeThingResponse xmlns="http://tempuri.org/">
<printsomeThingResult>string</printsomeThingResult>
</printsomeThingResponse>
</soap:Body>
</soap:Envelope>
ViewController.h
(IBAction)Invoke:(id)sender {
NSString *soapFormat =[NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-16\"?>\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"
"<printsomeThing xmlns=\"http://tempuri.org/\">\n"
"<userData>12</userData>"
"</printsomeThing>\n"
"</soap:Body>\n"
"</soap:Envelope>\n"];
NSLog(@"The request format is %@",soapFormat);
//location of the web method
NSURL *locationOfWebService = [NSURL URLWithString:@"http://tempuri.org/printsomeThing/Service1.asmx"];
NSLog(@"web url = %@",locationOfWebService);
//create connection request
NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc]initWithURL:locationOfWebService];
NSString *msgLength = [NSString stringWithFormat:@"%d",[soapFormat length]];
[theRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue:@"http://tempuri.org/printsomeThing" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
//the below encoding is used to send data over the net
[theRequest setHTTPBody:[soapFormat dataUsingEncoding:NSUTF8StringEncoding]];
//check if connection is established or not
NSURLConnection *connect = [[NSURLConnection alloc]initWithRequest:theRequest delegate:self];
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[webData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"ERROR with theConenction");
//[connection release];
//[webData release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"DONE. Received Bytes: %d", [webData length]);
NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
//NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF16StringEncoding];
NSLog(@"%@",theXML);
//[theXML release]
}
//recieving xml from the server
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
NSLog(@"didstart");
if([elementName isEqualToString:@"printsomeThingResult"]){
// if(!soapResults)
NSLog(@"Innnnn");
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
NSLog(@"foundcharacter");
[nodeContent appendString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
NSLog(@"didEndElement %@",nodeContent);
//if ([elementName isEqualToString:@"CelsiusToFahrenheitResult"]) {
if ([elementName isEqualToString:@"printsomeThingResult"]) {
finaldata = nodeContent;
//output.text = finaldata;
NSLog(@"Final Data is ===================== %@",finaldata);
}
//output.text = finaldata;
}
它说我的邮件已发送但我的网络服务没有收到它
2014-06-23 09:22:31.814 SoapServiceTest[3638:60b] The request format is <?xml version="1.0" encoding="utf-16"?>
<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>
<printsomeThing xmlns="http://tempuri.org/">
<userData>12</userData></printsomeThing>
</soap:Body>
</soap:Envelope>
2014-06-23 09:22:31.817 SoapServiceTest[3638:60b] web url = http://tempuri.org/printsomeThing/Service1.asmx
2014-06-23 09:22:32.436 SoapServiceTest[3638:60b] DONE. Received Bytes: 44115
2014-06-23 09:22:32.438 SoapServiceTest[3638:60b] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml"><head><meta content="text/html; charset=utf-8" http-equiv="content-type" /><script type="text/javascript">//<![CDATA[
si_ST=new Date
//]]></script><script type="text/javascript">//<![CDATA[
sb_gh=function(){return location.hash},sb_sh=function(n){location.hash=n};function _ge(n){return _d.getElementById(n)}_w=window,_d=document,sb_de=_d.documentElement,sb_ie=!!_w.ActiveXObject,sb_i6=sb_ie&&!_w.XMLHttpRequest,sb_st=_w.setTimeout,sb_ct=_w.clearTimeout,sb_gt=function(){return+new Date};sj_evt=new function(){function i(n){return t[n]||(t[n]=[])}var t={},n=this;n.fire=function(n){for(var r=i(n),u=r.e=arguments,t=0;t<r.length;t++)r[t].d?sb_st(sj_wf(r[t],u),r[t].d):r[t](u)},n.bind=function(n,t,r,u){var f=i(n);t.d=u,f.push(t),r&&f.e&&t(f.e)},n.unbind=function(n,i){for(var u=0,r=t[n];r&&u<r.length;u++)if(r[u]==i){r.splice(u,1);break}}};_G={ST:(si_ST?si_ST:new Date),Mkt:"en-CA",RTL:false,Ver:"9_00_0_3007771",IG:"8b539654167049fb97e266c2dce07427",EventID:"acb9ce0d1a43492083fd2e73c90b66f9",V:"homepage",P:"SERP",DA:"NYCr2v2",CID:"2CB5DC5900CE647E2293DA0601DF640F",SUIH:"TA00bzwuo-PiJ79CvDKBVA",PCId:"1",cUrl:"http:\/\/c.bing.com\/c.gif?DI=15074",akamaiSyncUrl:"http:\/\/cms.abmr.net\/pix?cid=1
我也使用了编码,因为soap消息字符集是utf 8,编码是16但是看起来确实有效
答案 0 :(得分:0)
您正在接收来自服务器的响应,如日志所示:
Received Bytes: 44115
所以,问题肯定在服务器上。 它回复,但请求没有路由到正确的页面,因此回复的页面是错误的页面。
检查theXML
的内容,以了解哪个页面正在回复,可能是某种404页面或类似内容。