2014-03-01 698 views
0
使用此代码的方法,数据上传到我的服务器

ASIFormDataRequest POST请求发出

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

IM

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

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

IM葛亭: {消息:请求的资源不支持HTTP方法“ GET'。}

我在做什么错误?

+0

如果这是新代码,请勿使用ASI,否则不再开发。 [ASIHTTP页面](http://allseeing-i.com/ASIHTTPRequest/)目前正在开发/支持的替代品是[AFNetworking](http://afnetworking.com)。 – zaph

回答

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

正如一句忠告,喜欢的东西AFNetworking更换ASI。 ASI不再被开发。

+0

很好回答。谢谢。给你一个upvote –