English 中文(简体)
连接到服务器
原标题:Connecting to the Server

我以前从未与web服务器接口,我对xml知之甚少,但对http头一无所知。我基本上需要连接到服务器并传递信息(使用Cocoa touch),这是提供的信息:

Request headers: 
POST https://www.example.org/example 
Content-Type: text/xml 
Content-Length: 638 
Authorization: [[developer-key]]

我收到了这个请求体:

<LetterValues> 
<FullName><![CDATA[John Doe]]></FullName>  
</LetterValues>

请求主体不仅仅是这个,我删除了很多只是为了保持简单。

那么,有人能给我指一个教程或正确的方向,告诉我如何传递这些信息吗?

最佳回答
-(void)xmlParsingInBackground
{
NSString xmlString = [NSString stringWithFormat:@"Your XML REquest"];
NSURL * serviceUrl = [NSURL URLWithString:[NSString stringWithString:@"YOUR URL"]];
NSMutableURLRequest * serviceRequest = [NSMutableURLRequest requestWithURL:serviceUrl];
[serviceRequest setValue:@"text/xml" forHTTPHeaderField:@"Content-type"];
[serviceRequest setHTTPMethod:@"POST"];
[serviceRequest setHTTPBody:[xmlString dataUsingEncoding:NSUTF8StringEncoding]];
NSData *responseData;
NSURLResponse * serviceResponse;
NSError * serviceError;
responseData = [NSURLConnection sendSynchronousRequest:serviceRequest returningResponse:&serviceResponse error:&serviceError];
NSString *resp=[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
if(responseData != NULL)
{       
    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:responseData];
    [xmlParser setDelegate:self];
    //[xmlParser setDelegate:parser];       
    BOOL success = [xmlParser parse];

    if(success)
    { // DO SOMETHING HERE
            }

}

做上面的事情,并实现以下内容:

 - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName 
attributes:(NSDictionary *)attributeDict 

 - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string  

 - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 

这将对您有所帮助。:)

添加:从以下链接下载代码,告诉您如何实现上述方法尺寸XML

问题回答

暂无回答




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签