Iphone 将JSON数据放入NSURL

Iphone 将JSON数据放入NSURL,iphone,objective-c,json,nsurl,Iphone,Objective C,Json,Nsurl,我正在尝试做一些似乎不需要动脑筋的事情,但由于某些原因,它不起作用 我试图对JSON数据发出get请求,在URL中发送一个JSON“data”参数 以下是我使用的代码: NSDictionary *whatToPost= [NSDictionary dictionaryWithObjectsAndKeys: username, @"username", password, @"password", nil]; NSString *url = [NSString stringWithFor

我正在尝试做一些似乎不需要动脑筋的事情,但由于某些原因,它不起作用

我试图对JSON数据发出get请求,在URL中发送一个JSON“data”参数

以下是我使用的代码:

NSDictionary *whatToPost= [NSDictionary dictionaryWithObjectsAndKeys:
 username, @"username", 
 password, @"password", nil];

NSString *url = [NSString stringWithFormat:@"http://domain.com/user/get?data=%@",
 [whatToPost JSONString]];
NSURL *theUrl = [NSURL URLWithString:
 [url stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
//NSLog(url) will produce this:
  http://domain.com/user/get?data={"username":"test1","password":"poop"}
设置断点时,URL为空。我不太明白它为什么会坏,但我想发送{或“'s”会坏它。有什么想法吗?我应该切换到POST吗

NSDictionary *whatToPost= [NSDictionary dictionaryWithObjectsAndKeys:
 username, @"username", 
 password, @"password", nil];

NSString *url = [NSString stringWithFormat:@"http://domain.com/user/get?data=%@",
 [whatToPost JSONString]];
NSURL *theUrl = [NSURL URLWithString:
 [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
你改变了方法

stringbyaddingpercentescapesusingencode:

返回使用给定编码的接收器表示形式,以确定将接收器转换为合法URL字符串所需的转义百分比

stringbyreplacingpercentescapesusingencode

返回一个新字符串,该字符串通过将接收方中的所有百分比转义替换为由给定编码确定的匹配字符而生成

请参阅:


您应该考虑在请求中发送HTTP身份验证头,而不是在URL本身中发送用户名和密码

比如: [request setValue:[WhatPost Description]用于HttpHeaderField:@“Authorization”]


当然,这是假设你控制了交易的另一端,即你正在使用的web服务。

你解决了我的问题!我试图了解出了什么问题!!!空url。还剩两个小时,我找到了这篇文章,谢谢!!!!!!!!!!