【问题标题】:ASIFormDataRequest POST Request IssueASIFormDataRequest POST 请求问题
【发布时间】:2014-03-01 14:29:29
【问题描述】:

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

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'。}

我做错了什么?

【问题讨论】:

  • 如果这是新代码,不要使用 ASI,它不再被开发。 ASIHTTP page 当前的替换是 AFNetworking,目前正在开发/支持。

标签: ios objective-c http post asiformdatarequest


【解决方案1】:

你把事情搞混了。您构建了一个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];

作为忠告,将 ASI 替换为 AFNetworking 之类的东西。 ASI 已不再开发。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多