【问题标题】:can't receive data from .net web server even it connect to .net web server即使连接到 .net Web 服务器也无法从 .net Web 服务器接收数据
【发布时间】:2010-12-20 06:06:07
【问题描述】:

我是 iphone 网络服务的新手。

我不喜欢这个tutorial

我的代码是休闲的

NSString *soapMsg = 
    [NSString stringWithFormat:
     @"<?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>"
     "<GenericAndroidMethod xmlns=\"Mortgage\">"
     "<methodName>MortgageGetLoanOfficerInfo</methodName>"
      "<xmlParam><MortgageGetLoanOfficerInfo><PhoneNumber>919703661366</PhoneNumber></MortgageGetLoanOfficerInfo></xmlParam>"
     "</GenericAndroidMethod>"
     "</soap:Body>"
     "</soap:Envelope>"
     ];
    NSLog(@"%@",soapMsg);
    NSURL *url = [NSURL URLWithString:@"http://10.10.8.7/MobileGenericWebservice/GenericWebService.asmx"];
    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]];
    [req addValue:@"text/xml; charset=utf-8"  
    forHTTPHeaderField:@"Content-Type"];
    [req addValue:@"Mortgage/GenericAndroidMethod"forHTTPHeaderField:@"SOAPAction"];
    [req addValue:msgLength 
    forHTTPHeaderField:@"Content-Length"];
    [req setHTTPMethod:@"POST"];
    [req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]];

    conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
    if (conn) {
        NSLog(@"connected %@",conn);
        webData = [[NSMutableData data] retain];
    }    
}

-(void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *) response {
    NSLog(@"connect %@",connection);
    [webData setLength: 0];
}


-(void) connection:(NSURLConnection *) connection didReceiveData:(NSData *) data {
    NSLog(@"test %@",data);
    [webData appendData:data];
}
-(void) connection:(NSURLConnection *) connection didFailWithError:(NSError *) error {
    NSLog(@"error will occer %@",error);
    [webData release];
    [connection release];
}
-(void) connectionDidFinishLoading:(NSURLConnection *) connection {
    NSLog(@"DONE. Received Bytes: %d", [webData length]);
    NSLog(@"recived data %@",webData);
    NSString *theXML = [[NSString alloc] 
                        initWithBytes: [webData mutableBytes] 
                        length:[webData length] 
                        encoding:NSUTF8StringEncoding];
    //---shows the XML---
    NSLog(@"%@",theXML);
    [theXML release];    


    [connection release];
    [webData release];
}

在控制台中我得到了

2010-12-20 11:23:50.754 Hello_SOAP[1911:40b] <?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><GenericAndroidMethod xmlns="Mortgage"><methodName>MortgageGetLoanOfficerInfo</methodName><xmlParam><MortgageGetLoanOfficerInfo><PhoneNumber>919703661366</PhoneNumber></MortgageGetLoanOfficerInfo></xmlParam></GenericAndroidMethod></soap:Body></soap:Envelope>
2010-12-20 11:23:50.963 Hello_SOAP[1911:40b] connected <NSURLConnection: 0x5d3dc30, http://10.10.8.7/MobileGenericWebservice/GenericWebService.asmx>
2010-12-20 11:23:51.375 Hello_SOAP[1911:40b] connect <NSURLConnection: 0x5d3dc30, http://10.10.8.7/MobileGenericWebservice/GenericWebService.asmx>
2010-12-20 11:23:51.376 Hello_SOAP[1911:40b] DONE. Received Bytes: 0
2010-12-20 11:23:51.377 Hello_SOAP[1911:40b] recived data <> 

在连接 didReceiveData 时我没有得到任何数据

谁能帮我解释一下为什么我没有收到数据。

提前谢谢你。

【问题讨论】:

  • 抱歉,工作量太大,阅读所有代码并试图弄清楚。你能缩小问题的范围,确切地解释哪里有问题吗?

标签: iphone


【解决方案1】:
NSString *soapMsg = 
[NSString stringWithFormat:
 @"<?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>"
 "<GenericAndroidMethod xmlns=\"Mortgage\">"
 "<methodName>string</methodName>"
 "<xmlParam>string</xmlParam>"
 "</GenericAndroidMethod>"
 "</soap:Body>"
 "</soap:Envelope>"
 ];
NSLog(@"%@",soapMsg);
NSURL *url = [NSURL URLWithString:@"http://173.31.193.92/MobileGenericWebservice/GenericWebService.asmx"];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]];
[req addValue:@"text/xml; charset=utf-8"  
forHTTPHeaderField:@"Content-Type"];
[req addValue:@"Mortgage/GenericAndroidMethod"forHTTPHeaderField:@"SOAPAction"];
[req addValue:msgLength 
forHTTPHeaderField:@"Content-Length"];
[req setHTTPMethod:@"POST"];
[req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]];

conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (conn) {
    NSLog(@"connected %@",conn);
    webData = [[NSMutableData data] retain];
}    
}

-(void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *)   response {
NSLog(@"connect %@",connection);
[webData setLength: 0];
}


-(void) connection:(NSURLConnection *) connection didReceiveData:(NSData *) data {
   // NSLog(@"test %@",data);
[webData appendData:data];
}
-(void) connection:(NSURLConnection *) connection didFailWithError:(NSError *) error {
NSLog(@"error will occer %@",error);
[webData release];
[connection release];
}
-(void) connectionDidFinishLoading:(NSURLConnection *) connection {
NSLog(@"DONE. Received Bytes: %d", [webData length]);
NSLog(@"recived data %@",webData);
NSString *theXML = [[NSString alloc] 
                    initWithBytes: [webData mutableBytes] 
                    length:[webData length] 
                    encoding:NSUTF8StringEncoding];
//---shows the XML---
NSLog(@"%@",theXML);
[theXML release];    


[connection release];
[webData release];
}

响应和日志如下:

2010-12-23 11:39:55.240 backGroundTest[1260:207] <?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><GenericAndroidMethod xmlns="Mortgage"><methodName>string</methodName><xmlParam>string</xmlParam></GenericAndroidMethod></soap:Body></soap:Envelope>
2010-12-23 11:39:55.245 backGroundTest[1260:207] connected <NSURLConnection: 0x4b4c220, http://173.31.193.92/MobileGenericWebservice/GenericWebService.asmx>
2010-12-23 11:39:56.105 backGroundTest[1260:207] connect <NSURLConnection: 0x4b4c220, http://173.31.193.92/MobileGenericWebservice/GenericWebService.asmx>
2010-12-23 11:39:56.106 backGroundTest[1260:207] DONE. Received Bytes: 530
2010-12-23 11:39:56.107 backGroundTest[1260:207] recived data <3c3f786d 6c207665 7273696f 6e3d2231 2e302220 656e636f 64696e67 3d227574 662d3822 3f3e3c73 6f61703a 456e7665 6c6f7065 20786d6c 6e733a73 6f61703d 22687474 703a2f2f 73636865 6d61732e 786d6c73 6f61702e 6f72672f 736f6170 2f656e76 656c6f70 652f2220 786d6c6e 733a7873 693d2268 7474703a 2f2f7777 772e7733 2e6f7267 2f323030 312f584d 4c536368 656d612d 696e7374 616e6365 2220786d 6c6e733a 7873643d 22687474 703a2f2f 7777772e 77332e6f 72672f32 3030312f 584d4c53 6368656d 61223e3c 736f6170 3a426f64 793e3c47 656e6572 6963416e 64726f69 644d6574 686f6452 6573706f 6e736520 786d6c6e 733d224d 6f727467 61676522 3e3c4765 6e657269 63416e64 726f6964 4d657468 6f645265 73756c74 3e266c74 3b4e6577 44617461 53657426 67743b0d 0a202026 6c743b47 55494454 61626c65 2667743b 0d0a2020 2020266c 743b4755 49442667 743b3635 37343664 33382d39 3464322d 34306433 2d613636 322d3633 31353765 65346263 6436266c 743b2f47 55494426 67743b0d 0a202026 6c743b2f 47554944 5461626c 65266774 3b0d0a26 6c743b2f 4e657744 61746153 65742667 743b3c2f 47656e65 72696341 6e64726f 69644d65 74686f64 52657375 6c743e3c 2f47656e 65726963 416e6472 6f69644d 6574686f 64526573 706f6e73 653e3c2f 736f6170 3a426f64 793e3c2f 736f6170 3a456e76 656c6f70 653e>
2010-12-23 11:39:56.108 backGroundTest[1260:207] <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GenericAndroidMethodResponse xmlns="Mortgage"><GenericAndroidMethodResult>&lt;NewDataSet&gt;
 &lt;GUIDTable&gt;
 &lt;GUID&gt;65746d38-94d2-40d3-a662-63157ee4bcd6&lt;/GUID&gt;
&lt;/GUIDTable&gt;
&lt;/NewDataSet&gt;</GenericAndroidMethodResult></GenericAndroidMethodResponse>  </soap:Body></soap:Envelope>

我相信问题出在 ip 设置上。请再检查一遍。

【讨论】:

  • 嘿,我想你忘了传递方法名称和 xml 参数
  • <MortgageGetLoanOfficerInfo><PhoneNumber>919703661366</PhoneNumber></MortgageGetLoanOfficerInfo>..这解决了问题..我命名了方法并且可以获得响应
  • 更好的方法是通过 CDATA 发送,因为它是您尝试发送的对象。919703661366 ]]>
  • 是的,你的天才感谢你的帮助。在你的帮助下,我明白了
【解决方案2】:

尝试从 didReceiveResponse 方法中查看标头

NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
if ([response respondsToSelector:@selector(allHeaderFields)]) {
    NSDictionary *dictionary = [httpResponse allHeaderFields];
    NSLog([dictionary description]);
}

查看说明。它可能会给出从服务器接收到的响应标头。它包含长度参数。如果为 0,则服务器不向应用程序返回任何数据。一种可能的解决方法是更改​​请求的标头。

【讨论】:

  • 是的,你是对的,我得到“Content-Length”= 0;你能帮我如何更改请求的标题
  • 尝试更改用户代理参数。而不是 NSURLRequest ,你需要提出 NSMutableUrlRequest。
  • [req setValue:userAgent forHTTPHeaderField:@"useragent"];
  • [req setValue:@"customAgent" forHTTPHeaderField:@"User-Agent"]; // 将“customAgent”字符串更改为其他任何内容并尝试
  • 173.31.193.92/MobileGenericWebservice/GenericWebService.asmx 这是我的网址,请您建议我如何从该网址获取数据
【解决方案3】:

请注意,连接 didReceiveData 没有被调用。从服务器发送响应时可能存在潜在错误。尝试从外部链接模拟soap 服务的相同测试参数并查看响应。您还可以在 didReceiveResponse 方法中通过提取 NSURLResponse 的响应代码来验证它。 我相信错误是由于发送的数据格式不正确造成的。

【讨论】:

  • 我通过直接提供相同的方法名称和 xml 婴儿车来测试我的 .net url,它返回 xml 数据。但它在我的应用程序中没有收到任何 xml 字符串。可能出了什么问题
  • 我也收到了来自服务器的响应
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
  • 2011-10-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多