iphone - 如何获取指定url的字符串?

标签 iphone ios xcode4 nsurlconnection

friend 们大家好,

我试图从网址获取字符串数据和图像数据。但是字符串数据

显示为空,谁告诉我如何将图片上传到指定的url。

我在这里编写这段代码

UIImage *image = [UIImage  imageWithData: [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://192.168.1.105:9966/PC/DisplayImage?id=2"]]];

myimages.image=image;
NSURL *url = [ NSURL URLWithString: [ NSString stringWithFormat:@"http://192.168.1.105:9966/PC/DisplayImage?id=2"] ]; 
NSData *imageData = UIImageJPEGRepresentation(image, 90);
NSString *encodedImage = [Base64 encode:imageData];
NSLog(@"my encoding image is %@",encodedImage);
NSURL *url = [ NSURL URLWithString: [ NSString stringWithFormat: @"http://192.168.1.105:9966/PC/DisplayImage?id=2"] ]; 
NSString *test = [NSString stringWithContentsOfURL:url];
NSLog(@"naveenkumar testing%@",test);

最佳答案

您可以使用 NSURLConnection 通过 post 方法上传图像 -

NSURL *postURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@submit.php",@"http://x.com/"]]; 

    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=\"text\"; filename=\"frontimage.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

        [postBody appendData:UIImageJPEGRepresentation(image, 90)];
        [postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// final boundary

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


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

    NSURLConnection *theConnection = [NSURLConnection connectionWithRequest:postRequest delegate:self];

    if (theConnection) 
        NSLog(@"%@",theConnection);

关于iphone - 如何获取指定url的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10025304/

相关文章:

ios - 如何从 AppDelegate.m 设置 tabBarItem 的角标(Badge)(tabBarView 不是 Root View )

iphone - 应用程序启动时进行应用内购买检查

iphone - 使用 uitextfields 创建 uitableview 表单

iphone - 越狱的 iPhone - root 权限

ios - 在 Swift 中,当应用程序进入后台时,如何在 AppDelegate 中验证 NSTimer?

iphone - iPhone 上的 REST API 问题

iphone - 如何确定 iOS 上的设置何时更改

iphone - 用于移动设备的 Twig 模板引擎

objective-c - 遇到问题,资源已被修改?

ios - 如何控制文本字段中文本的外观