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 ?
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.
Related
I am trying to do a task which I am completely not aware of, that is video uploading to server in objective c. I am a beginner and I was using swift, but now my requirement is in objective c.
Here I have to send an asynchronous request using multipart form data to server with three values. here is the data need to send in body:
body->form-data
projectId : String
sessionId : String
file : file
Its getting crashed at this line
" [urlRequest addValue:#"65" forHTTPHeaderField:#"projectId"];"
Can anyone help me to do this.
Thanks in advance.
NSString *str = [NSString stringWithFormat:#"http://abcdefghijkl.mnopqrst.com:8965/upload"];
NSURL *url = [NSURL URLWithString:str];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[urlRequest addValue:#"65" forHTTPHeaderField:#"projectId"];
[urlRequest addValue:#"43" forHTTPHeaderField:#"sessionId"];
[urlRequest addValue:#"" forHTTPHeaderField:#"file"];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (error) {
NSLog(#"Error,%#", [error localizedDescription]);
} else {
NSLog(#"%#", [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]);
}
}];
You should use NSMutableURLRequest
NSMutableURLRequest is a subclass of NSURLRequest that allows you to
change the request’s properties.
NSMutableURLRequest *mutableRequest = [request mutableCopy];
[mutableRequest addValue:#"65" forHTTPHeaderField:#"projectId"];
...
request = [mutableRequest copy];
i want to do a post request in afnetworking
this is my code whit ASI:
NSURL *mainurl = [NSURL URLWithString:#"xxxx/api/xxx/"];
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];
how can i do the same thong in AFnetworking ?
Not an exact answer, but here's a very similar POST request from my application:
-(void)setGpsLocation:(CLLocation*)location success:(TPScopeInterfaceSuccess)success failure:(TPScopeInterfaceFailure)failure
{
[_manager POST:#"gps/"
parameters:#{
#"lat":#(location.coordinate.latitude),
#"lon":#(location.coordinate.longitude),
#"height":#(location.altitude),
}
constructingBodyWithBlock:nil
success:success
failure:failure];
}
_manager is an instance of AFHTTPRequestOperationManager initialized as:
_manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:#"http://192.168.1.1/"]];
I am trying to verify a non-renewable subscription with Apple's sandbox server but keep getting back verify response: { "status":21002 } which means the request is malformed. Here is the relevant code I am using:
NSString *receiptString = [[NSString alloc] initWithData:transactionReceipt
encoding:NSUTF8StringEncoding];
NSString *encodedString = [receiptString base64Encoding];
NSString *jsonString = [NSString stringWithFormat:#"{ 'receipt-data' : '%#' }", encodedString];
NSURL *verificationURL = [NSURL URLWithString:#"https://sandbox.itunes.apple.com/verifyReceipt"];
__block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:verificationURL];
[request appendPostData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
[request setRequestMethod:#"POST"];
[request setCompletionBlock:^{
NSString *responseString = [request responseString];
NSLog(#"verify response: %#", responseString);
}];
[request setFailedBlock:^{
NSError *error = [request error];
NSLog(#"verify error: %#", [error description]);
}];
[request startAsynchronous];
Apparently the problem is the way I am sending the data to Apple through the ASIHTTPRequest library. Any insight on this appreciated. Thanks in advance!
Try to use another code. Example,
IAP_Validation
My request is
NSURL *requestUrl = [NSURL URLWithString:url];
if(request_) {
[request_ setDelegate:nil];
[request_ release];
}
request_ = [[ASIFormDataRequest requestWithURL:requestUrl] retain];
[request_ setDelegate:self];
[request_ setRequestMethod:#"GET"];
[request_ setTimeOutSeconds:HTTP_TIME_OUT];
[request_ startAsynchronous];
The call is going since the network access indicator on the top bar is rotating and disappearing after a few seconds. But either of the delegate methods
- (void)requestFinished:(ASIHTTPRequest *)request
or
- (void)requestFailed:(ASIHTTPRequest *)request
is not being called. What could have went wrong?
Thanks in advance.
PS: Sometimes, due to some issue, server is returning a "500 Internal Server Error". But even in that case, shouldn't the delegate
- (void)requestFailed:(ASIHTTPRequest *)request
be called?
you can use the blocks to check that out.
here is the example of the same:
NSURL *url = [NSURL URLWithString:#"http://allseeing-i.com"];
__block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setCompletionBlock:^{
// Use when fetching text data
NSString *responseString = [request responseString];
// Use when fetching binary data
NSData *responseData = [request responseData];
}];
[request setFailedBlock:^{
NSError *error = [request error];
}];
[request startAsynchronous];
I hope this might help.
Make sure your class is conforming to the ASIHTTPRequestDelegate protocol.
#interface YourClass : ... <ASIHTTPRequestDelegate>
I have writen the fellowing code:
NSString *urlString = [NSString stringWithFormat:ADDRESS,action];
postStr = #"user_name=Thomas Tan&phone=01234567891&password=123456";
NSData *myRequestData = [NSData dataWithBytes:[postStr UTF8String] length:[postStr length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:#"POST"];
[request setHTTPBody: myRequestData];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *responseString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSLog(#"%#",responseString);
it works well,but now I want to use asihttprequest framework,so how to change the above code,I have writen the code,but it can't get the correct result and just get the server error infomation.so what's the problem?
NSString *urlString = [NSString stringWithFormat:ADDRESS,action];
NSURL *url = [NSURL URLWithString:urlString];
ASIFormDataRequest *requeset = [ASIFormDataRequest requestWithURL:url];
[requeset setRequestMethod:#"POST"];
[requeset setPostValue:#"Thomas Tan" forKey:#"user_name"];
[requeset setPostValue:#"01234567891" forKey:#"phone"];
[requeset setPostValue:#"123456" forKey:#"password"];
[requeset startSynchronous];
NSError *error = [requeset error];
if (!error) {
NSString *re = [requeset responseString];
NSLog(#"%#",re);
}
NSLog(#"%#",error);
thank you in advance.
UPDATE:
NSString *urlString = [NSString stringWithFormat:ADDRESS,action];
NSURL *url = [NSURL URLWithString:urlString];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setRequestMethod:#"POST"];
[request appendPostData:[#"user_name=Thomas Tan&phone=01234567891&password=123456" dataUsingEncoding:NSUTF8StringEncoding]];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
NSString *re = [request responseString];
NSLog(#"%#",re);
}
NSLog(#"%#",error);
I use the above code ,It also can't get the same result,and error is not nil.
Your ASIHTTP code is not doing the same thing as your NSURLConnection code.
ASIFormDataRequest will automatically:
set the Content-Type header to application/x-www-form-urlencoded
URL-encoded your parameters
That's usually exactly what you want, but if you're getting the correct behavior with your NSURLConnection code and incorrect with ASIHTTP, then you need to change to a custom ASIHTTP POST and use ASIHTTPRequest, not ASIHTTPFormDataRequest, and then manually set the Conten-type back to application/x-www-form-urlencoded:
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setRequestMethod:#"POST"];
[request addRequestHeader:#"Content-Type" value:#"application/x-www-form-urlencoded"];
[request appendPostData:[#"user_name=Thomas Tan&phone=01234567891&password=123456" dataUsingEncoding:NSUTF8StringEncoding]];
Doing this, and inspecting exactly what was sent to the server using Wireshark, I can see that the POST data sent is still not quite identical (ASIHTTP on the left, NSURLConnection on the right):
But the content type, length, and actual data is identical.
At this point, I'd expect your server to return the same result.
If it still doesn't, you can edit the ASIhTTP request parameters to match.