[英]iphone post request headers
I am going to develop web services based on REST API. 我将基于REST API开发Web服务。 It is my first project of web services. 这是我的第一个Web服务项目。
I have a question regarding web services-iPhone app. 我对网络服务-iPhone应用程序有疑问。
Suppose when a user logged in using iPhone app his will get authentication token. 假设当用户使用iPhone应用程序登录时,他将获得身份验证令牌。
Using this token he/she wiil be authorized to use futher operation of app . 使用此令牌,他/她将被授权使用app的进一步操作。 I am planing about that iPhone app needs to be send hearders along with post/get request method with authentication key in herders. 我正在计划将iPhone应用程序与牧民中的身份验证密钥一起发送给听众以及发布/获取请求方法。
Request looks like: www.mydomaoin.com 请求看起来像:www.mydomaoin.com
But to authorize on server I need to pass special parameters to Header of request: 但是要在服务器上进行授权,我需要将特殊参数传递给请求的标头:
Headers: X-Testing-Auth-Secret: kI7wGju76kjhJHGklk76
Headers: Content-Type : application/json
which will be used in controller (zend framework) 将在控制器(zend框架)中使用
public function preDispatch()
{
$request = new Zend_Controller_Request_Http();
$key = $request->getHeader('X-Testing-Auth-Secret');
$type = $request->getHeader('Content-Type');
}
I don't know an iPhone capable for sending headers or not. 我不知道iPhone是否可以发送标题。 As as in case of android. 与android一样。
I am a php developer 我是php开发人员
Please help me 请帮我
You can use the following code 您可以使用以下代码
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];
There are some delegate methods you can use. 您可以使用一些委托方法。
-(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];
} }
是的,您可以从iPhone和Android手机发送标头
This is how you can append tokens in 'Post' request. 这样可以在“发布”请求中附加令牌。 Sample code : 样例代码:
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]];
Request for parsing xml data. 请求解析xml数据。
Suppose that is the method for request- 假设这是请求的方法-
-(void)PostPipeSeAamAnomaly { -(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");
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.