【问题标题】:consuming web service from iphone error从 iphone 错误使用 Web 服务
【发布时间】:2014-06-24 01:05:43
【问题描述】:

我一直在学习http://iphonebyradix.blogspot.ca/2011/04/working-with-webservices.html 教程,并决定使用 xaramin 创建自己的 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;
}

它说我的消息已发送,但我的网络服务没有收到它

    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,但这似乎也可以工作

【问题讨论】:

  • 如果你有 44KB 的数据发回给你,为什么你说你的服务器没有收到请求?
  • 因为我添加了一个 Console.WriteLine("test");在我的网络服务上,当我尝试从 iphone 访问它时,输出中没有显示任何内容
  • 我认为您的服务器有问题。它肯定会回复,但它不是您期望的服务器页面。检查您在theXML 中获得的输出,以了解返回给您的信息。
  • 哦,是的,它似乎正在从 web 服务上的我的 default.aspx 中打印出一些源代码,最后是一堆随机的 css
  • 好的,我输入了这个作为回复,所以你可以接受它并关闭问题。

标签: ios web-services


【解决方案1】:

您正在收到来自您的服务器的响应,正如日志所表明的那样:

Received Bytes: 44115

所以,问题肯定出在服务器上。 它回复了,但请求没有路由到正确的页面,所以回复的是错误的页面。

检查 theXML 的内容以了解正在回复的页面,可能是某种 404 页面或类似的页面。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-07
    • 2017-05-26
    • 2017-08-17
    相关资源
    最近更新 更多