1

我将开发基于 REST API 的 Web 服务。这是我的第一个 Web 服务项目。

我有一个关于网络服务-iPhone 应用程序的问题。

假设当用户使用 iPhone 应用程序登录时,他将获得身份验证令牌。

使用此令牌,他/她将被授权使用 app 的进一步操作。我正在计划 iPhone 应用程序需要发送听众以及带有身份验证密钥的 post/get 请求方法。

请求看起来像:www.mydomaoin.com

但是要在服务器上授权,我需要将特殊参数传递给请求的标头:

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 是否能够发送标头。就像安卓的情况一样。

我是一个php开发者

请帮我

4

5 回答 5

2

是的,您可以从 iPhone 和 Android 手机发送标头

于 2013-03-01T09:46:35.197 回答
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];

}

于 2013-02-21T07:04:37.643 回答
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];
于 2013-02-21T05:36:05.037 回答
1

请求解析 xml 数据。

假设这是请求的方法-

-(无效)PostPipeSeaAam异常{

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");
}

}

于 2013-02-21T06:57:24.857 回答
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]];
于 2013-02-21T05:35:31.487 回答