0

iPhone から に html リクエストを送信するにはどうすればよいですかservice url。html データ全体を文字列で取得し、のstring variable1 つとして渡す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]);
        }

しかし、エラーが発生して POST できません

提案/ヘルプは非常に高く評価されます

4

1 に答える 1

1

使用できますNSURLConnection: NSURLRequest:UserequestWithURL:(NSURL *)theURLを設定して、リクエストを初期化します。POSTリクエストやHTTPヘッダーを指定する必要がある場合はNSMutableURLRequest

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

を使用して 2 つの方法でリクエストを送信しますNSURLConnection

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

重要: UINSDataのブロックを回避するために、別のスレッドで同期要求を開始することを忘れないでください。

    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 に答える