【问题标题】:Consuming Web Service doesn't work使用 Web 服务不起作用
【发布时间】:2013-06-13 16:44:22
【问题描述】:

我在 php 中有一个肥皂网络服务,我试图从 Objective-C 中使用它,但我没有得到任何响应

请求正常吗?网络服务似乎工作正常..

这是我的 php 代码

    <?php
    require_once('lib/nusoap.php'); //include required class for build nnusoap web service server

      // Create server object
       $server = new soap_server();

       // configure  WSDL
       $server->configureWSDL('Upload File', 'urn:uploadwsdl');

       // Register the method to expose
        $server->register('cadastraUsuario');

        // Define the method as a PHP function

    function cadastraUsuario($_name, $_surname, $_location, $_email, $_password, $_gender, $_foto, $_tipo, $_dateLogin) {
            echo "tetdta";
        if($_name and $_surname)
        {
            $name       = strip_tags(trim(htmlentities($_name)));
            $surname    = strip_tags(trim(htmlentities($_surname)));
            $location   = strip_tags(trim(htmlentities($_location)));
            $email      = strip_tags(trim(htmlentities($_email)));
            $password   = strip_tags(trim(htmlentities($_password)));
            $gender     = strip_tags(trim(htmlentities($_gender)));
            $tipo       = strip_tags(trim(htmlentities($_tipo)));
            $_foto      = $_foto;

            //Consulta o email para ver se ja esta cadastrado
            $sql_consulta = "select * from `cdusers` where email = '".$email."'";   
            $result_consulta = mysql_query($sql_consulta);
            $row = mysql_num_rows($result_consulta);

            header('Content-type: text/xml');
            if($row > 0)
            {   
                print'
                <resultado>
                    <mensagem_cadastro>Este e-mail ja esta cadastrado!</mensagem_cadastro>
                </resultado>
                ';
            }
            else
            {
                $sql = "INSERT INTO `CDUsers` (`name`, `surname`, `location`, `email`, `password`, `gender`, `foto`, `tipo`, `dateLogin`) 
                        VALUES ('$name', '$surname', '$location', '$email', '$password', '$gender', '$foto', '$tipo', '')";

                $result = mysql_query($sql);

                //Se a execucao no banco estiver ok ou nao
                if($result)
                {
                    print '
                    <resultado>
                        <mensagem_cadastro>Usuário Cadastrado com sucesso!</mensagem_cadastro>
                    </resultado>
                    ';

                }
                else 
                    print '
                    <resultado>
                        <mensagem_cadastro>Erro ao cadastrar o ususario!</mensagem_cadastro>
                    </resultado>
                    ';
            }
        }
        else
        {
            echo "nao entrou na condição";
        }
    }



$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA); 

?>

这里是我在 Objective-C 中的代码

NSString *soapMessage = [NSString stringWithFormat:@"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"; xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"; xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"; xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\"; SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">";
                             "<SOAP-ENV:Body>\n"
                             "<cadastraUsuarioRequest>"
                             "<name>%@</name>"
                             "<surname>%@</surname>"
                             "<location>%@</location>"
                             "<email>%@</email>"
                             "<password>%@</password>"
                             "<gender>%@</gender>"
                            "<foto>%@</foto>"
                            "<tipo>m</tipo>"
                            "<dateLogin></dateLogin>"
                             "</cadastraUsuarioRequest>"
                             "</SOAP-ENV:Body>"
                             "</SOAP-ENV:Envelope>",txtName.text, txtSurName.text, txtLocation.text, txtEmail.text, txtPassword.text,gender,foto];

    NSLog(soapMessage);
    NSString *enderecoWS = @"http://www.url.it/cms/soap/server.php";
    NSURL *url = [NSURL URLWithString:enderecoWS];

    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://www.url.it/cms/soap/server.php/cadastraUsuario"; forHTTPHeaderField:@"SOAPAction"];
    [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
    [theRequest setHTTPMethod:@"POST"];
    [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

    theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

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

来自网络服务详细信息的方法:

关闭

名称:cadastraUsuario 绑定:WebServiceBinding 端点:

http://www.url.it/cms/soap/server.php SoapAction:

http://www.url.it/cms/soap/server.php/cadastraUsuario 风格:rpc

输入:使用:编码命名空间:encodingStyle:

http://schemas.xmlsoap.org/soap/encoding/消息:

cadastraUsuarioRequest 部分:输出:使用:编码命名空间:

encodingStyle:http://schemas.xmlsoap.org/soap/encoding/ 消息:

cadastraUsuarioResponse 部分:命名空间:传输:

http://schemas.xmlsoap.org/soap/http 文档:

【问题讨论】:

    标签: php objective-c ios4


    【解决方案1】:

    您的代码看起来正确,您是否实现了委托方法,例如:

    NSURL *url = [NSURL URLWithString:@"http://google.co.uk"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    (void) [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
    

    委托功能:

    -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
        NSLog(@"got response back");
    }
    
    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
        NSLog(@"http data: %@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
    }
    

    检查您的 Web 服务 HTTP 标头响应,它取决于其实现方式,但成功的请求应返回 201。有关其他代码,请参阅 here

    NSError *error;
    NSHTTPURLResponse *response;
    (void) [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
    NSLog(@"Response error: http response: %@", response.statusCode);    
    

    【讨论】:

    • 是的,我已经实现了......但没有得到答案......在receiveResponse和receiveData中......请看我的编辑
    • 你得到什么状态码?您是否使用 Mac 之类的客户端测试测试了您的请求 SOAP client
    • 我的意思是问题出在webservice
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-29
    • 2016-11-03
    • 2015-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多