上传到.NET Web服务iOS

时间:2015-03-02 05:14:22

标签: ios xml web-services wcf get

我需要传递一些数据才能让服务器保存信息,为了做到这一点,参数需要在URL http://xxx.xxx.xx.xxx/WCF/Service1.svc/XXX/Upload?sXML=

之后传递XML节点中的参数

以下是XML节点结构的示例

<Send_info>
<Service_order> 
</Service_order> 
<Data_stream> 
<info> 
</info> 
</Data_stream> 
</Send_info>.

返回值将是一个JSON字符串,分别为{“Uploaded”:“TRUE”}或{“Uploaded”:“FALSE”}。

我尝试过这种方法但没有回答它只返回nil。

-(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];
}

编辑:您可以在评论中找到答案

1 个答案:

答案 0 :(得分:0)

答案非常简单,只需使用这些代码块并用自己的参数替换它们

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

}