我将开发基于REST API的Web服务。 这是我的第一个Web服务项目。
我有一个关于网络服务的问题 - iPhone应用程序。
假设当用户使用iPhone应用程序登录时,他将获得身份验证令牌。
使用此令牌,他/她将被授权使用app的进一步操作。 我正在计划iPhone应用程序需要发送听众以及带有身份验证密钥的发布/获取请求方法。
请求看起来像: www.mydomaoin.com
但是要在服务器上进行授权,我需要将特殊参数传递给请求的Header:
Headers: X-Testing-Auth-Secret: kI7wGju76kjhJHGklk76
Headers: Content-Type : application/json
将用于控制器(zend框架)
public function preDispatch()
{
$request = new Zend_Controller_Request_Http();
$key = $request->getHeader('X-Testing-Auth-Secret');
$type = $request->getHeader('Content-Type');
}
我不知道iPhone是否能够发送标题。 与android的情况一样。
我是一名php开发人员
请帮帮我
答案 0 :(得分:2)
您可以使用以下代码
NSMutableURLRequest *req = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:url]];
NSString *post = @"your json";
//NSLog(@"%@",post);
NSData *postdata = [NSData dataWithBytes:[post UTF8String] length:[post length] ];
[req setHTTPMethod:@"POST"];
[req setValue:@"application/json" forHTTPHeaderField:@"content-type"];
[req setValue:@"kI7wGju76kjhJHGklk76" forHTTPHeaderField:@"Content-Length"];
[req setValue:[NSString stringWithFormat:@"%d",[postdata length]] forHTTPHeaderField:@"X-Testing-Auth-Secret"];
[req setHTTPBody:postdata];
(void)[NSURLConnection connectionWithRequest:req delegate:self];
答案 1 :(得分:2)
您可以使用一些委托方法。
-(void) parser:(NSXMLParser *) parser
didStartElement:(NSString *) elementName``
namespaceURI:(NSString *) namespaceURI
qualifiedName:(NSString *) qName
attributes:(NSDictionary *) attributeDict {
if( [elementName isEqualToString:@"AddGeneralInformationResult"])
{
if (!soapResults)
{
soapResults = [[NSMutableString alloc] init];
}
elementFound = YES;
}
}
-(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string
{
if (elementFound)
{
[soapResults appendString: string];
}
}
-(void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:@"AddGeneralInformationResult"])
{
elementFound = NO;
//NSLog(@"AddGeneralInformationResult %@",soapResults);
if([soapResults isEqualToString:@"true"])
{
[objDB updateDatabaseTable:@"GeneralInformation":@"Yes":
[[arrGenralInfo objectAtIndex:0] intValue]];
}
[soapResults setString:@""];
elementFound = FALSE;
}
}
-(void) connection:(NSURLConnection *) connection
didReceiveResponse:(NSURLResponse *) response {
[webData setLength: 0];
//NSLog(@"receive response");
}
-(void) connection:(NSURLConnection *) connection
didReceiveData:(NSData *) data {
[webData appendData:data];
}
-(void) connection:(NSURLConnection *) connection
didFailWithError:(NSError *) error {
//NSLog(@"Server error");
}
-(void) connectionDidFinishLoading:(NSURLConnection *) connection {
//NSLog(@"DONE. Received Bytes: %d", [webData length]);
NSString *theXML = [[NSString alloc]
initWithBytes: [webData mutableBytes]
length:[webData length]
encoding:NSUTF8StringEncoding];
//---shows the XML---
//NSLog(@"%@",theXML);
// [activityIndicator stopAnimating];
if (xmlParser)
{
}
xmlParser = [[NSXMLParser alloc] initWithData: webData];
[xmlParser setDelegate: self];
[xmlParser setShouldResolveExternalEntities:YES];
[xmlParser parse];
}
答案 2 :(得分:2)
是的,您可以从iPhone和Android手机发送标题
答案 3 :(得分:1)
这就是你可以在帖子中追加令牌的方法。请求。示例代码:
NSURL *url = [NSURL URLWithString:@"http://www.deveinvoice.sirus/process/user.asmx"];
req = [NSMutableURLRequest requestWithURL:url];
//---set the headers---
NSString *msgLength = [NSString stringWithFormat:@"%d",[strSoapMsg length]];
[req addValue:@"application/soap+xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[req addValue:@"http://tempuri.org/CheckTransactionIDExist" forHTTPHeaderField:@"SOAPAction"];
[req addValue:msgLength forHTTPHeaderField:@"Content-Length"];
//---set the HTTP method and body---
[req setHTTPMethod:@"POST"];
[req setHTTPBody: [strSoapMsg dataUsingEncoding:NSUTF8StringEncoding]];
答案 4 :(得分:1)
请求解析xml数据。
假设这是请求的方法 -
- (void)PostPipeSeAamAnomaly {
NSString *soapMsg =
[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:Body>"
"<PostPipeSeAamAnomaly xmlns=\"http://tempuri.org/\">"
"<PDAIRID>%d</PDAIRID>n"
"<AssociatedItemIDILI>%@</AssociatedItemIDILI>n"
"<AssociatedItemIDOther>%@</AssociatedItemIDOther>n"
"<RGW>%@</RGW>n"
"<StartDistance>%@</StartDistance>n"
"<Length>%@</Length>n"
"<Width>%@</Width>n"
"<Orientation1>%@</Orientation1>n"
"<Orientation2>%@</Orientation2>n"
"<AnomalyType>%@</AnomalyType>n"
"<OrientationToWeld>%@</OrientationToWeld>n"
"<LongestLinearIndication>%@</LongestLinearIndication>n"
"<LocalWallThickness>%@</LocalWallThickness>n"
"<AnomalyDepthMinimum>%@</AnomalyDepthMinimum>n"
"<AnomalyDepthAverage>%@</AnomalyDepthAverage>n"
"<AnomalyDepthMaximum>%@</AnomalyDepthMaximum>n"
"<AnomalyDepthSurface>%@</AnomalyDepthSurface>n"
"<AnomalyDepthMaximumPercentage>AA</AnomalyDepthMaximumPercentage>n"
"<AnomalyDepthMinimumPercentage>AA</AnomalyDepthMinimumPercentage>n"
"<SizingMethod>%@</SizingMethod>n"
"<AnomalyRemoved>%@</AnomalyRemoved>n"
"<RemedialActionRequired>%@</RemedialActionRequired>n"
"<RemedialActionIncidental>%@</RemedialActionIncidental>n"
"<Comment>%@</Comment>n"
"</PostPipeSeAamAnomaly>n"
"</soap:Body>"
"</soap:Envelope>",PDAirID ,[arrPipeSeamAnomaly objectAtIndex:2] ,[arrPipeSeamAnomaly objectAtIndex:3],[arrPipeSeamAnomaly objectAtIndex:4] ,[arrPipeSeamAnomaly objectAtIndex:5] ,[arrPipeSeamAnomaly objectAtIndex:6],[arrPipeSeamAnomaly objectAtIndex:7] ,[arrPipeSeamAnomaly objectAtIndex:8] ,[arrPipeSeamAnomaly objectAtIndex:9],[arrPipeSeamAnomaly objectAtIndex:10],[arrPipeSeamAnomaly objectAtIndex:11] ,[arrPipeSeamAnomaly objectAtIndex:12] ,[arrPipeSeamAnomaly objectAtIndex:13],[arrPipeSeamAnomaly objectAtIndex:2] ,[arrPipeSeamAnomaly objectAtIndex:2],[arrPipeSeamAnomaly objectAtIndex:14] ,[arrPipeSeamAnomaly objectAtIndex:15],[arrPipeSeamAnomaly objectAtIndex:16] ,[arrPipeSeamAnomaly objectAtIndex:17],[arrPipeSeamAnomaly objectAtIndex:18],[arrPipeSeamAnomaly objectAtIndex:19] ,[arrPipeSeamAnomaly objectAtIndex:20]];
//NSLog(@"%@",soapMsg);
NSURL *url = [NSURL URLWithString:
WEBSERVICEURL];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
//---set the various headers---
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]];
[req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[req addValue:@"http://tempuri.org/PostPipeSeAamAnomaly" forHTTPHeaderField:@"SOAPAction"];
[req addValue:msgLength forHTTPHeaderField:@"Content-Length"];
//---set the HTTP method and body---
[req setHTTPMethod:@"POST"];
[req setHTTPBody:[soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
//[activityIndicator startAnimating];
conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (conn) {
webData = [NSMutableData data];
//NSLog(@"go to delegateMethods");
}
}