一、用URLSession请求
NSString *soapStr = [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:Header>\ </soap:Header>\ <soap:Body>\ <login xmlns=\"targetNameSpace地址/\">\ <param1>test</param1>\ <param2>test</param2>\ </login>\ </soap:Body>\ </soap:Envelope>"]; NSURL *url=[NSURL URLWithString:@"你自己wsdl文档中对应的endpoint address"]; NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url]; // 访问方式 [request setHTTPMethod:@"POST"]; // 设置请求头(请求头也可以不设置,前两个设不设置都一样,应该默认的,但是SOAPAction我怎么都设置不对,不设置也可以,干脆不设置了) // [request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; // [request addValue:[NSString stringWithFormat:@"%zd", soapStr.length] forHTTPHeaderField:@"Content-Length"]; // [request addValue:@"nameSpace/methodName" forHTTPHeaderField:@"SOAPAction"]; // body内容 [request setHTTPBody:[soapStr dataUsingEncoding:NSUTF8StringEncoding]]; NSURLSession *session = [NSURLSession sharedSession]; NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; WJLog(@"进入成功回调Session-----结果:%@----请求地址:%@", result, response.URL); if (error) { WJLog(@"Session----失败----%@", error.localizedDescription); } }]; [task resume];
二、用AFNetworking请求
一开始想用manager发请求,直接把XML当params发POST肯定是直接挂了,于是想要设置HTTPBody要不用AFHTTPRequestOperation?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
NSMutableURLRequest
*request=[
NSMutableURLRequest
requestWithURL:url];
[request setHTTPMethod:@
"POST"
];
[request setHTTPBody:[soapStr dataUsingEncoding:
NSUTF8StringEncoding
]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
// 设置返回数据格式
operation.responseSerializer = [AFHTTPResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation * _Nonnull operation,
id
_Nonnull responseObject) {
NSString
*result = [[
NSString
alloc] initWithData:responseObject encoding:
NSUTF8StringEncoding
];
WJLog(@
"AFN--成功--结果:%@----返回数据%@"
, result, responseObject);
} failure:^(AFHTTPRequestOperation * _Nonnull operation,
NSError
* _Nonnull error) {
|