iphone загрузить картинку https post - PullRequest
0 голосов
/ 23 декабря 2011

Я пытаюсь загрузить изображение с помощью https методом post.

Я уже знаю, как загрузить изображение на http-сервер.

Чтобы загрузить изображение как https, япросто нужно добавить добавить s на http?

большое спасибо

ex:

NSString *urlString = @"http://192.168.10.30:8080/thek_save/JSP/file_upload.jsp";
// setting up the request object now
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

Ответы [ 2 ]

1 голос
/ 23 декабря 2011

Вы можете использовать тот же код загрузки изображения в HTTP на POST. просто замените запуск "HTTP://" на" HTTPS://". На HTTPS-соединении должен быть установлен действительный сертификат SSL

0 голосов
/ 23 декабря 2011

Пожалуйста, прочитайте этот код ... после того, как вы прочитали этот код, вы знаете, что "как использовать почтовое соединение" .... означает загрузку изображения через почтовое соединение и использование метода делегата

-(void) sendImageToServer
{

        NSURL *postURL = [NSURL URLWithString:[NSString   stringWithFormat:@"@"http://192.168.10.30:8080/thek_save/JSP/file_upload.jsp";]]; 

        NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:postURL
                                                                   cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                               timeoutInterval:30.0];

        // change type to POST (default is GET)
        [postRequest setHTTPMethod:@"POST"];

        NSMutableData *postBody = [NSMutableData data];


        // just some random text that will never occur in the body
        NSString *stringBoundary = @"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo";

        // header value
        NSString *headerBoundary = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary];

        // set header
        [postRequest addValue:headerBoundary forHTTPHeaderField:@"Content-Type"];


        // image ------------

        [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [postBody appendData:[@"Content-Disposition: form-data; name=\"image\"; filename=\"image.png\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        [postBody appendData:[@"Content-Type: image/png\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        [postBody appendData:[@"Content-Transfer-Encoding: binary\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        // add it to body


        NSData *data = UIImageJPEGRepresentation(img, 90);

        [postBody appendData:data];
        [postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

    /// UIImage *testImage = [UIImage imageWithData:data];


        // final boundary
        [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];


        // add body to post
        [postRequest setHTTPBody:postBody];

        NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:postRequest delegate:self];

        if( theConnection )
        {
            webData = [NSMutableData data] ;
        }
        else
        {
            NSLog(@"theConnection is NULL");
        }

    }

///Use Delegete
#pragma mark -
#pragma mark ConnectionDelegate
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [webData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"ERROR with theConenction");

}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"DONE. Received Bytes: %d", [webData length]);
    NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
    NSLog(@"%@",theXML);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...