简体   繁体   English

在iPhone中使用POST将包含XML数据的变量发布到Web服务器

[英]post variable containing XML data to a web-server using POST in iPhone

I've looked through a lot of opened questions within similar topic but unfortunately couldn't find the answer. 我浏览了类似主题内的许多悬而未决的问题,但不幸的是找不到答案。 The thing is I'm new to sending POST/GET messages and I'm not sure How do I need to POST variable containing XML data to a web-server. 事情是我刚开始发送POST / GET消息,但不确定如何将包含XML数据的POST变量发送到Web服务器。

"Use POST or GET variable "test" with XML string on a specified URL". “在指定的URL上对XML字符串使用POST或GET变量“ test””。

I know how to make connection, and put XML into HTTPBody and make a request. 我知道如何建立连接,并将XML放入HTTPBody并发出请求。 But I don't know how to specify XML for a variable. 但是我不知道如何为变量指定XML。

Please help. 请帮忙。

If by "specify XML for a variable" you mean send an XML string as part of multipart/form-data, then it's easy. 如果通过“为变量指定XML”是指将XML字符串作为multipart / form-data的一部分发送,那么这很容易。

You just have to append your XML string to your request body like you do, but additionaly encapsulate it between boundaries and add a content header. 您只需要像您一样将XML字符串附加到请求主体,即可,另外将其封装在边界之间并添加内容标头。

NSURL *remoteURL = [NSURL URLWithString:@"http://someurl"];
NSMutableURLRequest *imageRequest = [[NSMutableURLRequest alloc] initWithURL:remoteURL];
//A unique string that will be repeated as a separator
NSString *boundary = @"14737809831466499882746641449";

//this is important so the webserver knows that you're sending multipart/form-data
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[imageRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];

NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; //boundary
[body appendData:[@"Content-Disposition: form-data; name=\"xmlString\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; //your content header
[body appendData:[xmlString dataUsingEncoding:NSUTF8StringEncoding]]; //your content itself
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[imageRequest setHTTPMethod:@"POST"]; //set Method as POST
[imageRequest setHTTPBody:body];

NSData *data = [NSURLConnection sendSynchronousRequest:imageRequest returningResponse:nil error:nil];

Or... If you want send a GET variable as part of your query string, just URL encode it and add it as part of your URL. 或者...如果要发送GET变量作为查询字符串的一部分,只需对URL进行编码,然后将其添加为URL的一部分即可。

NSString *xmlString = @"<xml>...</xml>";
NSString *escapedString = [xmlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *urlString = [NSString stringWithFormat:@"http://someserver/?xmlString=%@",escapedString];
NSURL *url = [NSURL URLWithString:urlString];
NSLog(@"Current URL: %@", url);

This is what a request URL with a HTTP GET parameter looks like. 这就是带有HTTP GET参数的请求URL的样子。

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM