ios - ASIFormDataRequest POST 请求问题

标签 ios objective-c http post asiformdatarequest

我正在使用此代码将数据发布到我的服务器

NSURL *mainurl = [NSURL URLWithString:@"http://xxxxxxxxxx/api/PhonePaymentApi/Transaction/"];

    NSString * postdata = [[NSString alloc]initWithFormat:@"UniqueId=%@&jsonProduct=%@&BranchId=%@&OrderToTime=%@",GETUnicidentifire,JsonOrderDetail,BranchId,OrderToTime];

    ASIFormDataRequest *requestt = [ASIFormDataRequest requestWithURL:mainurl];

    [requestt setRequestMethod:@"POST"];
    [requestt addRequestHeader:@"application/x-www-form-urlencoded" value:@"Content-Type"];
    [requestt appendPostData:[postdata dataUsingEncoding:NSUTF8StringEncoding]];

    NSString * theurl = [NSString stringWithFormat:@"%@",mainurl];
    NSURLRequest *thereqest = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:theurl]];
    [NSURLConnection connectionWithRequest:thereqest delegate:self];

在方法中

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

    NSLog(@"%@",error); 
}

我得到: {消息:请求的资源不支持 http 方法“GET”。

我做错了什么?

最佳答案

你在这里搞混了。您构建了一个 ASIFormDataRequest,但实际上并没有发送它。您发送的是 NSURLConnection

我已经有很长时间没有使用 ASI 了,但这可能会有所帮助:

NSURL *mainurl = [NSURL URLWithString:@"http://xxxxxxxxxx/api/PhonePaymentApi/Transaction/"];

ASIFormDataRequest *requestt = [ASIFormDataRequest requestWithURL:mainurl];

[requestt addPostValue:GETUnicidentifire forKey:@"UniqueId";
[requestt addPostValue:JsonOrderDetail   forKey:@"jsonProduct";
[requestt addPostValue:BranchId          forKey:@"BranchId";
[requestt addPostValue:OrderToTime       forKey:@"OrderToTime";

[requestt setCompletionBlock:^{
    // Process the response
}];
[requestt setFailedBlock:^{
    NSError *error = [requestt error];
    NSLog(@"%@",error);
}];

[requestt startAsynchronous];

作为建议,用 AFNetworking 之类的东西替换 ASI。 ASI 不再开发。

关于ios - ASIFormDataRequest POST 请求问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22115587/

相关文章:

ios - 当我在 url 中包含空格时 AFHTTPRequestSerializer 中的 AfNetworking 失败

ios - 我可以将使用 ARC 的静态库链接到面向 iOS 3.2 的项目吗?

ios - UITextFieldDelegate : How to select which methods gets called for each individual TextField

c++ - 从 Objective-C 函数调用 C++ 函数不起作用

javascript - EventSource 连接中的自定义 header

python - 当我从空闲状态导入 `http.server` 时它可以工作,但是当我运行具有 `import http.server` 的 python 文件时出现错误

ios - swift 将数据发送到Apple Watch

ios - 在 keyboardWillShow keyboardWillHide 中同步动画——同时使用硬件键盘和虚拟键盘

ios - 全局计算属性线程安全吗?

javascript - $Http.get(),如何处理来自WebAPI的404响应