上传到.NET Web服务的iOS
问题描述:
我需要为了传递一些数据,服务器到存储的信息,为了做到这一点的参数需要传递XML节点参数如下的URL http://xxx.xxx.xx.xxx/WCF/Service1.svc/XXX/Upload?sXML=上传到.NET Web服务的iOS
下面是XML节点结构
<Send_info>
<Service_order>
</Service_order>
<Data_stream>
<info>
</info>
</Data_stream>
</Send_info>.
返回值将是一个JSON字符串与{“上传”:“TRUE”}的一例{:“FALSE”,“上载”}分别或。
我试过这个方法没有回答它只是返回零。
-(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
}
GET:
-(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
}