无聊。周末写几个APP玩,终于在上午把网络折腾好了。5块钱的8139网卡让我时常能在MAC下上网。
图标简陋啊。。。
技术要点:
1。组一个SOAP包。用NLURLConnection连接。
2。找一个webservice。http://webservice.webxml.com.cn
3。解析XML。
4。显示,拖拽控件。
随便贴点组SOAP包头,发送的Code
NSString
*
soapMessage
=
[NSString stringWithFormat:
@" <?xml version=\ " 1.0 \ " encoding=\ " utf - 8 \ " ?>\n "
" <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/ \">\n"
" <soap:Body>\n "
" <getMobileCodeInfo xmlns=\ " http: // WebXml.com.cn/\">\n"
" <mobileCode>%@</mobileCode>\n "
" <userID></userID>\n "
" </getMobileCodeInfo>\n "
" </soap:Body>\n "
" </soap:Envelope>\n " , nameInput.text
];
NSLog(soapMessage);
NSURL * url = [NSURL URLWithString: @" http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx " ];
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://WebXml.com.cn/getMobileCodeInfo " forHTTPHeaderField: @" SOAPAction " ];
[theRequest addValue: msgLength forHTTPHeaderField: @" Content-Length " ];
[theRequest setHTTPMethod: @" POST " ];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection * theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate :self];
@" <?xml version=\ " 1.0 \ " encoding=\ " utf - 8 \ " ?>\n "
" <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/ \">\n"
" <soap:Body>\n "
" <getMobileCodeInfo xmlns=\ " http: // WebXml.com.cn/\">\n"
" <mobileCode>%@</mobileCode>\n "
" <userID></userID>\n "
" </getMobileCodeInfo>\n "
" </soap:Body>\n "
" </soap:Envelope>\n " , nameInput.text
];
NSLog(soapMessage);
NSURL * url = [NSURL URLWithString: @" http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx " ];
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://WebXml.com.cn/getMobileCodeInfo " forHTTPHeaderField: @" SOAPAction " ];
[theRequest addValue: msgLength forHTTPHeaderField: @" Content-Length " ];
[theRequest setHTTPMethod: @" POST " ];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection * theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate :self];
XML解析,用的是NSXMLParse。还好。比较墨迹。
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName
attributes: (NSDictionary
*
)attributeDict
{
if ( [elementName isEqualToString: @" getMobileCodeInfoResult " ])
{
if ( ! soapResults)
{
soapResults = [[NSMutableString alloc] init];
}
}
}
- ( void )parser:(NSXMLParser * )parser foundCharacters:(NSString * ) string
{
if ( recordResults )
{
[soapResults appendString: string ];
}
}
- ( void )parser:(NSXMLParser * )parser didEndElement:(NSString * )elementName namespaceURI:(NSString * )namespaceURI qualifiedName:(NSString * )qName
{
if ( [elementName isEqualToString: @" getMobileCodeInfoResult " ])
{
resultOutPut.text = soapResults;
[soapResults release];
soapResults = nil;
}
}
{
if ( [elementName isEqualToString: @" getMobileCodeInfoResult " ])
{
if ( ! soapResults)
{
soapResults = [[NSMutableString alloc] init];
}
}
}
- ( void )parser:(NSXMLParser * )parser foundCharacters:(NSString * ) string
{
if ( recordResults )
{
[soapResults appendString: string ];
}
}
- ( void )parser:(NSXMLParser * )parser didEndElement:(NSString * )elementName namespaceURI:(NSString * )namespaceURI qualifiedName:(NSString * )qName
{
if ( [elementName isEqualToString: @" getMobileCodeInfoResult " ])
{
resultOutPut.text = soapResults;
[soapResults release];
soapResults = nil;
}
}
防止盗链,正文要mark
http://alexliu.cnblogs.com