方法一:直接将参数用&拼接在调用的接口之后,然后Request请求即可。
NSString * putSFC(const char * url,int timout)
{}
int SF_Login()
{
NSString *ret;
NSString *url = @"http://10.10.42.10:8080/WebService/Wip_TestSvr.asmx/UserLogin";
NSString *username = [CTestContext::m_dicGlobal objectForKey:@"SF_Username"];
NSString *password = [CTestContext::m_dicGlobal objectForKey:@"SF_Password"];
const char *msg = [[NSString stringWithFormat:@"%@?User=%@&Password=%@",url,username,password] UTF8String];
sprintf(tmper, "-----msg=%s",msg);
SaveDebug(tmper,__func__,__LINE__);
ret = putSFC(msg, 5);
if ([ret isEqual: @"OK"]) {
return 0;
}
else{
return -1;
}
}
方法二:将参数拼写成xml格式然后发送。
second method 学习至http://my.oschina.net/plumsoft/blog/75277。
NSString *webServiceBodyStr = [NSString stringWithFormat:
@"
"
"
"
ret = putxml(@"UserLogin", webServiceBodyStr, 5);
NSString *putxml(NSString *action,NSString* xml,int timeout)
{
NSURL * strURL = [NSURL URLWithString:@"http://10.10.42.10:8080/WebService/Wip_TestSvr.asmx"];
if (!strURL)
{
NSLog(@"Invalid URL : %s","http://10.10.42.10:8080/WebService/Wip_TestSvr.asmx");
return @"Invalid URL";
}
NSString *webServiceStr = [NSString stringWithFormat:@"\n"
"
"
"%@\n"
"
"
NSString *SOAPActionStr = [NSString stringWithFormat:@"http://10.10.42.10:8080/WebService/Wip_TestSvr.asmx/%@",action];
NSString *msgLength = [NSString stringWithFormat:@"%ld", xml.length];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:strURL];
[theRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest addValue:SOAPActionStr forHTTPHeaderField:@"SOAPAction"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody:[webServiceStr dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *con = [[NSURLConnection alloc]initWithRequest:theRequest delegate:nil];
// NSURLSession *session = [NSURLSession sharedSession];
// NSURLSessionDataTask *task = [session dataTaskWithRequest:theRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
// NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// NSLog(@"run into callback session , result = %@ , url = %@",result,response.URL);
// if(error){
// NSLog(@"session fail -> %@",error.localizedDescription);
// }
//
// }];
// [task resume];
if (con) {
msgData = [NSMutableData data];
return @"OK";
}
else
{
return @"GG";
}
}
-(void) connectionDidFinishLoading:(NSURLConnection *) connection 完成接受数据的时候在此函数内解析
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *) data 接受的数据添加到msgData内
{
[msgData appendData:data];
}