参数发送到Web服务
在我开始:我编程的iPhone,用客观的C.参数发送到Web服务
我已经执行到使用的NSURLRequest和NSURLConnection的网络服务功能的调用。该函数然后返回一个XML与我需要的信息。
的代码如下:
NSURL *url = [NSURL URLWithString:@"http://myWebService/function"];
NSMutableURLRequest theRequest = [[NSMutableURLRequest alloc] initWithURL:url];
NSURLConnection theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
我也实现的方法
- didRecieveResponse
- didRecieveAuthenticationChallenge
- didRecievedData
- didFailWithError
- connectionDidFinishLoading。
它的功能完美。
现在我需要发送2个参数到函数:“位置”和“模块”。
我尝试使用以下修改:
NSMutableURLRequest theRequest = [[NSMutableURLRequest alloc] initWithURL:url];
[theRequest setValue:@"USA" forHTTPHeaderField:@"location"];
[theRequest setValue:@"DEVELOPMENT" forHTTPHeaderField:@"module"];
NSURLConnection theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
但它似乎并没有工作。 我做错了什么?有没有办法知道我是否使用了错误的参数名称(也许它是“位置”或“位置”或无关紧要?)? 还是有办法知道哪些参数是等待功能...
额外的信息:
我没有访问网络服务的来源,所以我不能修改它。 但我可以访问WSDL。做这个功能的人说的都在那里......但我无法理解它。 < ...
任何帮助,将不胜感激。 :)
几个例子GET邮政SOAP
GET请求
GET /index.html?userid=joe&password=guessme HTTP/1.1
Host: www.mysite.com
User-Agent: Mozilla/4.0
发布采购信息
POST /login.jsp HTTP/1.1
Host: www.mysite.com
User-Agent: Mozilla/4.0
Content-Length: 27
Content-Type: application/x-www-form-urlencoded
userid=joe&password=guessme
SOAP请求
POST /InStock HTTP/1.1
Host: www.example.org
Content-Type: application/soap+xml; charset=utf-8
Content-Length: nnn
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
<soap:Body xmlns:m="http://www.example.org/stock">
<m:GetStockPrice>
<m:StockName>IBM</m:StockName>
</m:GetStockPrice>
</soap:Body>
</soap:Envelope>
的主机,用户代理,内容 - 长度,钴内容类型是请求标头中的项目
如果涉及到WSDL,它可能是一个SOAP Web服务,所以它可能期望一些SOAPy XML作为输入,我不认为你这样做。看看这个问题可能是:How to access SOAP services from iPhone
您正在设置HTTP头中的值,而不是HTTP请求中的GET或POST参数。这可能不是你想要做的。
如果服务器接受GET请求参数,你也许能够做这样的事在URL:
"http://myWebService/function?location=USA&module=DEVELOPMENT"
如果做不到这一点,你必须去充分的SOAP路线为MK说。
谢谢,我会尝试这第一nn – 2010-04-06 14:49:43
@Ajjandra注意:我只是纠正了一个错误的URL字符串:应该是一个“?”之前的参数,不是“;”因为我有 – 2010-04-06 14:56:22
无法正常工作,似乎我需要使用SOAP。 – 2010-04-06 15:25:54
花了我几个小时才得到这个工作,所以我想我会发布我的解决方案,以便将来可以节省别人的一些时间。这就是我如何通过使用字符串参数从.NET WCF服务中获取JSON数据的Objective-C。
(我使用Wireshark来捕获.NET客户端说话WCF服务,所以我知道我需要什么,从我的iOS客户端传递)
static NSString *feedURLString = @"http://www.mydomain.com/DownloadService.svc";
NSMutableURLRequest *faURLRequest =
[NSMutableURLRequest requestWithURL:[NSURL URLWithString:feedURLString]];
[faURLRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[faURLRequest setHTTPMethod:@"POST"];
[faURLRequest addValue:@"http://tempuri.org/IDownloadService/GetChanges" forHTTPHeaderField:@"SOAPAction"];
NSString *localLastSync;
if (userProfile.lastSync == nil)
{
localLastSync = @"2011-09-01T12:00:00";
}
else
{
localLastSync = userProfile.lastSync;
}
NSString *soapMessage = [NSString stringWithFormat:
@"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
"<s:Body >\n"
"<GetChanges xmlns=\"http://tempuri.org/\">\n"
"<lastSync>%@</lastSync>\n"
"</GetChanges>\n"
"</s:Body>\n"
"</s:Envelope>\n", localLastSync];
[faURLRequest setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
self.downloadConnection =
[[NSURLConnection alloc] initWithRequest:faURLRequest delegate:self];
没错!我认为headerFields不是做这个的地方...但我绝望XD – 2010-04-06 14:51:17
如果你可以访问WSDL,你可能想看看wsdl2objc:http://code.google.com/p/wsdl2objc/ – 2010-05-24 20:46:55