Wcf服务发布数据IOS

时间:2013-12-26 20:39:43

标签: ios objective-c wcf web-services

我是网络服务的新手。我正在尝试向Web服务发送一些参数。

这是我的服务链接http://quizmedb.somee.com/Service.svc?wsdl

我的服务中有bool signUp(string username, string name, string surname, string mail, string pass)这个方法。

我使用此代码发布我的数据。

NSString *soapMessage = [NSString stringWithFormat:
                         @"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
                         "<SOAP-ENV:Envelope \n"
                         "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" \n"
                         "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n"
                         "xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" \n"
                         "SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" \n"
                         "xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"> \n"
                         "<SOAP-ENV:Body> \n"
                         "<signUp xmlns=\"http://tempuri.org/\"><username>%@</username><name>%@</name><surname>%@</surname><mail>%@</mail><pass>%@</pass>"
                         "</signUp> \n"
                         "</SOAP-ENV:Body> \n"
                         "</SOAP-ENV:Envelope>",@"RandomName",nameTextField.text,surnameTextField.text,emailTextField.text,passTextField.text];

NSURL *url = [NSURL URLWithString:@"http://quizmedb.somee.com/Service.svc?wsdl"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"http://tempuri.org/Service/signUp" forHTTPHeaderField:@"Soapaction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];     
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

if(theConnection) {
    webData = [NSMutableData data];
}
else {
    NSLog(@"theConnection is NULL");
}

}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse           *)response{

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{

}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
 NSLog(@"DONE. Received Bytes: %d", [webData length]);


 NSString *strData = [[NSString alloc]initWithData:webData encoding:NSUTF8StringEncoding];
 NSLog(@"result : %@",strData);

}

但我无法从服务中获取任何字符串。什么是我的错,我找不到。  你能帮助我吗?  感谢您的回答和兴趣。

1 个答案:

答案 0 :(得分:1)

执行此代码时是否收到任何错误?

顺便说一句,在我看来,你的肥皂动作不正确,

[theRequest addValue: @"http://tempuri.org/Service/signUp" forHTTPHeaderField:@"Soapaction"];

根据wsdl,它应该是,

[theRequest addValue: @"http://tempuri.org/IService/signUp" forHTTPHeaderField:@"Soapaction"];

希望它能解决您的问题。 :d

我用肥皂ui尝试了你的wsdl,你的请求信息没有正确形成。这就是它没有返回的原因。 您可以在代码中更改这样的请求消息格式:

        <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
            <soapenv:Header/>
             <soapenv:Body>
                    <tem:signUp>
                        <tem:username>%@</tem:username>
                        <tem:ad>%@</tem:ad>
                        <tem:soyad>%@</tem:soyad>
                        <tem:mail>%@</tem:mail>
                        <tem:pass>%@</tem:pass>
                    </tem:signUp>
            </soapenv:Body>
        </soapenv:Envelope>

或进行测试,您也可以使用以下请求消息

        <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
            <soapenv:Header/>
             <soapenv:Body>
                    <tem:signUp>
                   </tem:signUp>
            </soapenv:Body>
        </soapenv:Envelope>

它会在回复后返回给您。

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body>
      <signUpResponse xmlns="http://tempuri.org/">
      <signUpResult>true</signUpResult>
      </signUpResponse>
   </s:Body>
</s:Envelope>