0

如何将 html 请求从 iPhone 发送到service url。我可以将整个 html 数据放入字符串中并string variable作为其中之一传递xml tag and POST request吗?我需要任何转换吗?

我的html请求是这样的

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Untitled Document</title>
        </head>
        
        <body style="background:#bdd9ef; padding:20px 0px; margin:0px;">
            <table width="700px" cellspacing="0" cellpadding="0" style="border-radius:10px; margin:0px auto; background:white; color:#2222; padding:20px 0px;" >
        
................
................

我正在通过以下方式进行操作:

 NSString *url=[NSString stringWithFormat:@"http://XXX/services/Email.svc"];
        NSMutableURLRequest *request=[[[NSMutableURLRequest alloc] init]autorelease];
        [request setURL:[NSURL URLWithString:url]];
        [request setHTTPMethod:@"POST"];
        NSString *contentType=[NSString stringWithFormat:@"text/xml"];
        [request addValue:contentType5 forHTTPHeaderField:@"Content-Type"];
        NSMutableData *postBody=[NSMutableData data];
        [postBody appendData:[[NSString stringWithFormat:@"<EmailServiceRequest xmlns=\"http://schemas.datacontract.org/2004/07/\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">"]dataUsingEncoding:NSUTF8StringEncoding]];
         [postBody appendData:[[NSString stringWithFormat:@"<HtmlBody>%@</HtmlBody>",htmldata]dataUsingEncoding:NSUTF8StringEncoding]];
          [postBody appendData:[[NSString stringWithFormat:@"</EmailServiceRequest>"]dataUsingEncoding:NSUTF8StringEncoding]];
        [request setHTTPBody:postBody];
        NSHTTPURLResponse *urlResponse=nil;
        NSError *error=nil;
        NSData *responseData = [NSURLConnection sendSynchronousRequest:request
                                                      returningResponse:&urlResponse
                                                                  error:&error];

        if (responseData!= NULL)
        {
            NSString *rss = [[NSString alloc] initWithData:responseData
                                                   encoding:NSUTF8StringEncoding ];
            NSLog(@"Response Code:%d",[urlResponse statusCode]);
            if([urlResponse statusCode ]>=200 && [urlResponse statusCode]<300)
            {
                NSLog(@"Response:%@",rss);
            }
        }
        else
        {
            NSLog(@"Failed to send request: %@", [error localizedDescription]);
        }

但我收到错误并且无法发布

任何建议/帮助都将不胜感激

4

1 回答 1

1

您可以使用NSURLConnection:设置您的NSURLRequest:使用requestWithURL:(NSURL *)theURL来初始化请求。如果您需要指定POST请求和/或HTTP标头,请使用NSMutableURLRequestwith

    (void)setHTTPMethod:(NSString *)method
    (void)setHTTPBody:(NSData *)data
    (void)setValue:(NSString *)value forHTTPHeaderField:(NSString *)field

使用以下两种方式发送您的请求NSURLConnection

    Synchronously: (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)

这将返回一个NSData您可以使用的变量重要提示:请记住在单独的线程中启动同步请求以避免阻塞 UI。

    Asynchronously: (void)start

不要忘记设置您的NSURLConnection's委托来处理连接,如下所示:

    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [self.data setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)d {
    [self.data appendData:d];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    [[[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error", @"")
                                 message:[error localizedDescription]
                                delegate:nil
                       cancelButtonTitle:NSLocalizedString(@"OK", @"") 
                       otherButtonTitles:nil] autorelease] show];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSString *responseText = [[NSString alloc] initWithData:self.data encoding:NSUTF8StringEncoding];

    // Do anything you want with it 

    [responseText release];
}

// Handle basic authentication challenge if needed
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
    NSString *username = @"username";
    NSString *password = @"password";

    NSURLCredential *credential = [NSURLCredential credentialWithUser:username
                                                             password:password
                                                          persistence:NSURLCredentialPersistenceForSession];
    [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
}
于 2013-01-08T07:18:22.080 回答