ASIFormDataRequest POST Request Issue

情到浓时终转凉″ 提交于 2019-12-10 12:22:52

问题


im using this code to post data to my server

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];

in the method

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

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

im geting: {Message:The requested resource does not support http method 'GET'.}

what im doing Wrong ?


回答1:


You are mixing things up here. You build an ASIFormDataRequest, but you don't actually send it. What you do send is an NSURLConnection.

It's been a long time since I've used ASI, but this might help:

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];

As a word of advice, replace ASI with something like AFNetworking. ASI is no longer being developed.



来源:https://stackoverflow.com/questions/22115587/asiformdatarequest-post-request-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!