cocoa - 如何在 Cocoa 中发出 POST 请求?

标签 cocoa

这是我的代码,我的代码正在工作,但我的问题是当我尝试在 NSString *postString = @"username=example&firSTLastname=example"; 上传递变量而不是名称时,上面写着 example我想要 = TextField 值,那么我该怎么做呢?我尝试过发布字符串附加字符串,但它不起作用。请帮忙

NSURL *aUrl= [NSURL URLWithString:@"http://xxxx/iqueueinsertinjoinq.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
NSString *postString = @"username=example&firstlastname=example";

[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];

//NSURLConnection *connection= [[NSURLConnection alloc] initWithRequest:request delegate:self];
 [[NSURLConnection alloc] initWithRequest:request delegate:self ];}

最佳答案

如果我正确理解你的问题,你想要的是这样的: 变化:

NSString *postString = @"username=example&firstlastname=example";

致:

NSString *username = @"foo";
NSString spokenname = @"bar";
NSString *postString = [NSString stringWithFormat:@"username=%@&firstlastname=%@", username, spokenname];

NSString 的 stringWithFormat:允许在格式字符串中进行变量替换。在我的示例中,“username”将是包含用户名的 NSString 变量,“spokenname”是名字姓氏字符串。

关于cocoa - 如何在 Cocoa 中发出 POST 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19243166/

相关文章:

objective-c - cocoa - 在 NSView 中绘制图像

xcode - 在主菜单中放置一个窗口 xCode

ios - UITabBarController 似乎在关闭另一个 View 之前打开一个 View

macos - 带有预设行的静态 NSTableView

cocoa - Lion 中是否已弃用工具栏项目分隔符?

ios - 使用 Objective-c 获取 X-GM-LABELS(Gmail 标签)

cocoa - 是否可以以编程方式隐藏停靠栏图标

objective-c - 子类化 NSTextField 以创建号码拨号文本字段

iphone - 触摸移动时将底 View 移至顶 View

objective-c - 如何构造一个非常大的 Objective-C 类?