上传到 .NET Web 服务 iOS
Upload to .NET web service iOS
我需要传递一些数据以便服务器保存信息,为此,参数需要在 url [=] 之后的 XML 个节点中传递参数 http://xxx.xxx.xx.xxx/WCF/Service1.svc/XXX/Upload?sXML=
这里是XML节点结构的例子
<Send_info>
<Service_order>
</Service_order>
<Data_stream>
<info>
</info>
</Data_stream>
</Send_info>.
return 值将是一个 JSON 字符串,分别包含 {"Uploaded":"TRUE"} 或 {"Uploaded":"FALSE"} .
我试过这个方法,没有答案只是return没有。
-(void)Request:(NSData*)data{
NSURL *aUrl = [NSURL URLWithString:@"http://xxx.xxx.xx.xxx/WCF/Service1.svc/XXX/Upload?sXML="];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request setHTTPMethod:@"GET"];
[request setHTTPBody:data];
NSURLConnection *connection= [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
}
编辑:您可以在评论中找到答案
答案非常简单,只需使用这些代码块并用您自己的参数替换它们
POST:
-(void)webservicepost:(NSString *)poststring{
NSString *poststring = @"String in the format we need to upload";
NSURL *remoteURL = [NSURL URLWithString:@"URL in which we'll upload"];
NSMutableURLRequest *Request = [[NSMutableURLRequest alloc] initWithURL:remoteURL];
[Request addValue:@"application/json; charset=utf-8" forHTTPHeaderField: @"Content-Type"];//Formats for the data
NSMutableData *body = [NSMutableData data];
[body appendData:[poststring dataUsingEncoding:NSUTF8StringEncoding]]; //Add content and coding
[Request setHTTPMethod:@"POST"]; //POST method
[Request setHTTPBody:body];
NSData *data = [NSURLConnection sendSynchronousRequest:Request returningResponse:nil error:nil];//Server response in data form
NSString *sdata = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];//Server response in string form
}
获取:
-(void)get:(NSString *)myxmlstring{
NSString *escapedString = [myxmlstring stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *urlString = [NSString stringWithFormat:@"http://xxx.xxx.xx.xxx/WCF/Service1.svc/XXX/Upload?sXML=%@",escapedString];
NSURL *url = [NSURL URLWithString:urlString];
NSData *data = [NSURLConnection sendSynchronousRequest:Request returningResponse:nil error:nil];//Server response in data form
NSString *sdata = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];//Server response in string form
//FROM HERE YOU CAN USE THE RETRIEVED INFORMATION FROM THE URL
}
我需要传递一些数据以便服务器保存信息,为此,参数需要在 url [=] 之后的 XML 个节点中传递参数 http://xxx.xxx.xx.xxx/WCF/Service1.svc/XXX/Upload?sXML=
这里是XML节点结构的例子
<Send_info>
<Service_order>
</Service_order>
<Data_stream>
<info>
</info>
</Data_stream>
</Send_info>.
return 值将是一个 JSON 字符串,分别包含 {"Uploaded":"TRUE"} 或 {"Uploaded":"FALSE"} .
我试过这个方法,没有答案只是return没有。
-(void)Request:(NSData*)data{
NSURL *aUrl = [NSURL URLWithString:@"http://xxx.xxx.xx.xxx/WCF/Service1.svc/XXX/Upload?sXML="];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request setHTTPMethod:@"GET"];
[request setHTTPBody:data];
NSURLConnection *connection= [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
}
编辑:您可以在评论中找到答案
答案非常简单,只需使用这些代码块并用您自己的参数替换它们
POST:
-(void)webservicepost:(NSString *)poststring{
NSString *poststring = @"String in the format we need to upload";
NSURL *remoteURL = [NSURL URLWithString:@"URL in which we'll upload"];
NSMutableURLRequest *Request = [[NSMutableURLRequest alloc] initWithURL:remoteURL];
[Request addValue:@"application/json; charset=utf-8" forHTTPHeaderField: @"Content-Type"];//Formats for the data
NSMutableData *body = [NSMutableData data];
[body appendData:[poststring dataUsingEncoding:NSUTF8StringEncoding]]; //Add content and coding
[Request setHTTPMethod:@"POST"]; //POST method
[Request setHTTPBody:body];
NSData *data = [NSURLConnection sendSynchronousRequest:Request returningResponse:nil error:nil];//Server response in data form
NSString *sdata = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];//Server response in string form
}
获取:
-(void)get:(NSString *)myxmlstring{
NSString *escapedString = [myxmlstring stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *urlString = [NSString stringWithFormat:@"http://xxx.xxx.xx.xxx/WCF/Service1.svc/XXX/Upload?sXML=%@",escapedString];
NSURL *url = [NSURL URLWithString:urlString];
NSData *data = [NSURLConnection sendSynchronousRequest:Request returningResponse:nil error:nil];//Server response in data form
NSString *sdata = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];//Server response in string form
//FROM HERE YOU CAN USE THE RETRIEVED INFORMATION FROM THE URL
}