0

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

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

我在做什么错?

4

1 回答 1

2

你在这里把事情搞混了。您构建了一个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 不再被开发。

于 2014-03-01T15:05:23.947 回答