使用Web服务不起作用

时间:2013-06-17 16:32:15

标签: php objective-c ios4

我在php中有一个soap webservice,我正在尝试从Objective-C中使用它,但我没有得到任何响应

请求还可以吗? web服务似乎工作正常..

这是我在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");
    }
}
来自webservice详细信息的

方法:

  

关闭

     

名称:cadastraUsuario绑定:WebServiceBinding端点:

     

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

     

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

     

输入:use:encoded namespace:encodingStyle:

     

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

     

cadastraUsuarioRequest parts:输出:use:encoded namespace:

     

encodingStyle:http:/ /schemas.xmlsoap.org/soap/encoding/ message:

     

cadastraUsuarioResponse parts:命名空间:Transport:

     

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

1 个答案:

答案 0 :(得分:0)

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

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);