作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
<分区>
我是 Xcode 的新手,我需要连接到 Web 服务 API (SOAP + DES),这意味着我必须先向服务器发送用户名、密码、 key 和 IV,然后才能获取 XML 文件,然后解析它。
是否有关于从 iPhone 连接到 Web 服务 API(SOAP + DES)的教程?
-(IBAction)buttonClick:(id)sender
{
recordResults = NO;
NSString *soapMessage = [NSString stringWithFormat:
@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance" "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema" "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
"<soap:Header>"
"<CredentialSoapHeader xmlns=\"http://tempuri.org/\">"
"<userID>xxxxx</userID>"
"<Password>xxxx</Password>"
"</CredentialSoapHeader>"
"</soap:Header>"
"<soap:Body>"
"<GetNearUserByArticleLatLon xmlns=\"http://tempuri.org/\">"
"<strXML>"
"<XMLDATA>\n"
"<KeyWord></KeyWord>\n"
"<Lat>0</Lat>\n"
"<Lon>0</Lon>\n"
"<IP>219.71.65.55</IP>\n"
"<CurrentPage>1</CurrentPage>\n"
"<PageSize>10</PageSize>\n"
"</XMLDATA>\n"
"</strXML>"
"</GetNearUserByArticleLatLon>"
"</soap:Body>"
"</soap:Envelope>"];
NSLog(@"%@", soapMessage);
//http://www.xignite.com/services/GetRealQuote
NSURL *url = [NSURL URLWithString:@"http://api3.mmnears.com/API/api.asmx?op=GetNearUserByArticleLatLon"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
[theRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue:@"http://tempuri.org/GetNearUserByArticleLatLon" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
NSLog(@"theConnection = %@",theConnection);
if(theConnection)
{
webData = [[NSMutableData data] retain];
NSLog(@"WebData = %@",webData);
}
else
{
NSLog(@"theConnection is null");
}
}
-(void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response
{
[webData setLength:0];
NSHTTPURLResponse * httpResponse;
httpResponse = (NSHTTPURLResponse *) response;
NSLog(@"HTTP error %zd", (ssize_t) httpResponse.statusCode);
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data
{
[webData appendData:data];
NSLog(@"webdata: %@", data);
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError*)error
{
NSLog(@"error with the connection");
[connection release];
[webData release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"DONE. Received bytes %d", [webData length]);
NSString *theXML = [[NSString alloc] initWithBytes:[webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
NSLog(@"xml %@",theXML);
[theXML release];
我是一名优秀的程序员,十分优秀!