2014-06-23 5 views
0

자습서 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 메시지 charset은 utf 8이고 인코딩은 16이지만 어느 쪽도 작동하는 것으로 보입니다

+0

44KB 데이터가 다시 전송되면 서버가 요청을받지 못하는 이유는 무엇입니까? 내가 Console.WriteLine ("test")을 추가했기 때문에 – sergio

+0

; 내 웹 서비스에서 아무 것도 출력에 나타나면 iphone에서 액세스하려고합니다. –

+0

서버에 문제가 있다고 생각합니다. 물론 답장을 보내지 만 예상 한 서버 페이지가 아닙니다. 'theXML'에서 얻는 결과를 검사하여 어떤 정보가 여러분에게 돌아올지를 이해하십시오. – sergio

답변

0

요 귀하의 서버에서 로그를 통해 평이한 응답을 받았습니다.

Received Bytes: 44115 

확실히 서버에 문제가 있습니다. 답장을 보내지 만 요청이 올바른 페이지로 라우트되지 않으므로 응답하는 페이지가 잘못되었습니다.

theXML의 내용을 검토하여 어떤 페이지가 404 페이지 또는 그와 비슷한 종류의 페이지로 회신하는지 파악하십시오.

관련 문제