方法一:直接将参数用&拼接在调用的接口之后,然后Request请求即可。
NSString * putSFC(const char * url,int timout)
{NSURL * strURL = [NSURL URLWithString:[NSString stringWithUTF8String:url]];
if (!strURL)
{
NSLog(@"Invalid URL : %@",[NSString stringWithUTF8String:url]);
return @"Invalid URL";
}
NSURLRequest* request = [NSURLRequest requestWithURL:strURL cachePolicy:(NSURLRequestCachePolicy)0 timeoutInterval:timout];
NSURLResponse* response=nil;
NSError* error=nil;
NSData* data=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (data==nil) {
sprintf(tmper, "-----request=%s;data=%s",[[NSString stringWithFormat:@"%@",request] UTF8String],[[NSString stringWithFormat:@"%@",data] UTF8String]);
SaveDebug(tmper,__func__,__LINE__);
return @"empty data";
}
sprintf(tmper, "-----request=%s;data=%s",[[NSString stringWithFormat:@"%@",request] UTF8String],[[NSString stringWithFormat:@"%@",data] UTF8String]);
SaveDebug(tmper,__func__,__LINE__);
NSString * str = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
if (str==nil) {
return @"empty str";
}
NSLog(@"SFC Response : %@",str);
return str;//[str UTF8String];
}
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:
@"<UserLogin xmlns=\"http://10.10.42.10:8080/WebService/Wip_TestSvr.asmx/\">"
"<User>%@</User>"
"<Password>%@</Password>"
"</UserLogin>",username,password];//这里是参数
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:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<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/\">\n"
"<soap:Body>\n"
"%@\n"
"</soap:Body>\n"
"</soap:Envelope>",xml];
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];
}