2014-06-23 59 views
0

我一直在關注教程http://iphonebyradix.blogspot.ca/2011/04/working-with-webservices.html,並決定使用xaramin創建自己的Web服務並嘗試調用它。但它似乎並沒有因某種原因而被召喚。從iPhone使用Web服務錯誤

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; 
} 

它說,我的消息得到了發送,但我的web服務沒有收到它雖然

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 

我與編碼發揮各地以及自肥皂消息字符集是UTF 8,它的編碼是16,但似乎工作要麼

+0

爲什麼你說你的服務器沒有收到請求,如果你有44KB的數據發回給你? – sergio

+0

,因爲我加了一個Console.WriteLine(「test」);在我的網絡服務,並沒有出現在輸出時,當我嘗試從iPhone訪問它 –

+0

我認爲你有一個問題在您的服務器上。它絕對回覆,但它不是你期望的服務器頁面。檢查你在'XML'中得到的輸出,以瞭解什麼信息會回傳給你。 – sergio

回答

0

喲ü正在收到你的服務器的迴應,如日誌所示:

Received Bytes: 44115 

所以,絕對是服務器上的問題。 它回覆,但請求沒有路由到正確的頁面,所以它是錯誤的頁面回覆。

檢查theXML的內容以瞭解哪個頁面正在回覆,可能是某種404頁面或類似內容。