background image

26

                          

"</soap:Body>"

  

27

                          

"</soap:Envelope>"

,province];  

28

      

29

    NSLog(@

"%@"

,soapString);  

30

    

//soap 请求地址

  

31

    NSURL *url=[[NSURL alloc] 

initWithString:@

"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"

];  

32

    

//请求

  

33

    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];  

34

    

//设置请求头部

  

35

    

////设置 ContentType

  

36

    [request addValue:@

"text/xml; charset=utf-8"

 forHTTPHeaderField:@

"Content-

Type"

];  

37

    

////设置 SOAPAction

  

38

    [request addValue:@

"http://WebXml.com.cn/getWeatherbyCityName" 

forHTTPHeaderField:@

"SOAPAction"

];  

39

    

//设置 Content-length

  

40

    [request addValue:[NSString stringWithFormat:@

"%d"

,[soapString length]] 

forHTTPHeaderField:@

"Content-Length"

];  

41

    

//

 

设置请求类型 POST 或 GET

  

42

    [request setHTTPMethod:@

"POST"

];  

43

    

//设置请求 Body(只有 post 方式有)

  

44

    [request setHTTPBody:[soapString dataUsingEncoding:NSUTF8StringEncoding]];  

45

    

//连接

  

46

    NSURLConnection *connection=[NSURLConnection connectionWithRequest:request 

delegate:self];  

47

      

48

    

if

 (connection) {  

49

        webData=[NSMutableData data];  

50

    }  

51

}  

 
(2) SOAP 1.2(除了 soap 不一样外,其他都一样)
以下是请求实例:

52

POST /WebServices/WeatherWebService.asmx HTTP/1.1  

53

Host: www.webxml.com.cn  

54

Content-Type: application/soap+xml; 

charset

=

utf

-8  

55

Content-Length: length  

56

   

57

<?xml

 

version

=

"1.0"

 

encoding

=

"utf-8"

?>

  

58

<soap12:Envelope

 

xmlns:xsi

=

"http://www.w3.org/2001/XMLSchema-instance" 

xmlns:xsd

=

"http://www.w3.org/2001/XMLSchema"