• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - iPhone上的错误URL,当我在浏览器中输入它时它可以工作

[复制链接]
菜鸟教程小白 发表于 2022-12-13 00:24:40 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在制作一个 jsonstring。当我执行它时,当我在浏览器中执行它时它就可以工作。我通过记录确切的 url 并将其复制到浏览器中来做到这一点。比我得到我想要的 HTTP Get,但在 iPhone 中我只得到一个错误的登录。

- (IBAction)getDownid)sender { //perform get request
    NSLog(@"beginnen met versturen");

    //NSString * _barCode = [[NSUserDefaults standardUserDefaults] objectForKey"phoneNumber"];

    //build up the request that is to be sent to the server
    //NSString*jsonString = [[NSString alloc] initWithFormat"{\"barcode\":\"%@\"}", _barCode];


    NSString*jsonString = [[NSString alloc] initWithFormat"{\"barcode\":\"123456\"}"];

    NSString *str = [NSString stringWithFormat: @"http://server.nl/scan.php?data=%@",jsonString];
    NSLog(@"%@", str);
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL    URLWithString:str]];
    NSLog(@"url: %@", request);

    [request setHTTPMethod"GET"];
   // [request addValue"getValues" forHTTPHeaderField"METHOD"]; //selects what task the server will perform
    NSLog(@"met value: %@", request);

    //initialize an NSURLConnection  with the request
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    if(!connection){
        NSLog(@"Connection Failed");
    }

}

-(void)connectionNSURLConnection *)connection didReceiveDataNSData *)data{ // executed when the connection receives data

    receivedData = data;
    /* NOTE: if you are working with large data , it may be better to set recievedData as NSMutableData
     and use  [receivedData appendata] here, in this event you should also set recievedData to nil
     when you are done working with it or any new data received could end up just appending to the
     last message received*/
}
-(void)connectionNSURLConnection *)connection didFailWithErrorNSError *)error{ //executed when the connection fails

    NSLog(@"Connection failed with error: %@",error.localizedDescription);
}
-(void)connectionNSURLConnection *)connection didReceiveAuthenticationChallengeNSURLAuthenticationChallenge *)challenge{

    /*This message is sent when there is an authentication challenge ,our server does not have this requirement so we do not need to handle that here*/
}
-(void)connectionDidFinishLoadingNSURLConnection *)connection{

    NSLog(@"Request Complete,recieved %d bytes of data",receivedData.length);

    //[self.delegate requestReturnedData:receivedData];//send the data to the delegate
    NSData *data = receivedData;
    NSDictionary *dictionary = [NSDictionary dictionaryWithJSONData:data];
    NSLog(@"%@",dictionary.JSONString ); ; // set the textview to the raw string value of the data recieved

    NSString *value1 = [dictionary objectForKey"barcode"];
    NSLog(@"%@", value1);
    NSString *value2 = [dictionary objectForKey"product"];
    NSLog(@"%@",dictionary);
    NSLog(@"%@", value2);

}

这是日志:

2013-01-10 16:31:46.550 Scanner[14875:907] http://server.nl/scan.php?data={"barcode":"123456"}
2013-01-10 16:31:46.551 Scanner[14875:907] url: <NSMutableURLRequest (null)>
2013-01-10 16:31:46.553 Scanner[14875:907] met value: <NSMutableURLRequest (null)>
**2013-01-10 16:31:46.556 Scanner[14875:907] Connection failed with error: bad URL**

当我从字符串中删除完整的 json 时,我没有得到错误的 url。所以可能有问题。有谁知道我做错了什么?



Best Answer-推荐答案


在执行 URL 请求之前,您需要对其进行编码。 最好和最优雅的解决方案是在 NSString 上添加一个类别,例如:

- (NSString*)URLEncode {
    // Should not be encoded:-_.
    return [(NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)self, NULL, CFSTR(";/?&=+$,!*'()<>#%\"{}|\\^[]`~"), kCFStringEncodingUTF8) autorelease];
//
}

当你提出请求时:

NSString *str = [NSString stringWithFormat: @"http://server.nl/scan.php?data=%@",jsonString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL    URLWithString:[str URLEncode]];

如果您不想使用其他文件(甚至认为会推荐),请将此方法添加到您的类中:

- (NSString*)URLEncodeNSString )yourURL {
 // Should not be encoded:-_. 
return [(NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)self, NULL, CFSTR(";/?&=+$,!'()<>#%\"{}|\\^[]`~"), kCFStringEncodingUTF8) autorelease]; 
} 

并使用

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[self URLEncode:str]]; 

关于ios - iPhone上的错误URL,当我在浏览器中输入它时它可以工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14261548/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap