Log into website with ASIHttpRequest - iphone

I have a website that i want to log into from my iphone in a iphone app i am making, i followed the documentation on ASIHttpRequests website but all i get from the response string is the html code for the login page , but i get a OK http code, Why is this happening?
Here is my code :
-(IBAction)fetchData:(id)sender
{
NSURL *url = [NSURL URLWithString:#"http://www.rssit.site90.com/login.php"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request shouldPresentCredentialsBeforeChallenge];
[request setRequestMethod:#"POST"];
[request setPostValue:#"" forKey:#"username"];
[request setPostValue:#"" forKey:#"password"];
[request setDelegate:self];
[request startAsynchronous];
//Add finish or failed selector
[request setDidFinishSelector:#selector(requestLoginFinished:)];
[request setDidFailSelector:#selector(requestLoginFailed:)];
}
- (void)requestLoginFinished:(ASIHTTPRequest *)request
{
NSString *yourResponse = [request responseString];
NSLog(#"%#", yourResponse);
}

The form on that page is doing a get, not a post. Your server is probably not expecting POST data, and is looking at query string params instead. Change to
[NSURL URLWithString:#"http://www.rssit.site90.com/login.php?username=YOURUSERNAME&password=YOURPASSWORD"];
and set the requestMethod to GET.

Related

cURL web services from iphone

I need to fetch data from curl web services from iOS, the client provided me a header name, header value and a base url, I dont know what to do with them, the url isn't giving me anything opening in a browser. They mentioned data is JSON encoded.
Please link me to some library or tutorial on how to call them.
You can use ASIHTTPRequest for cURL as well.
what I have used is:
NSURL *url = [NSURL URLWithString:url];
__weak ASIHTTPRequest* request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
request.shouldPresentCredentialsBeforeChallenge = YES;
[request appendPostData:[JSONString dataUsingEncoding:NSUTF8StringEncoding]];
[request setUsername:username];
[request setPassword:password];
[request setRequestMethod:#"PUT"];
request.timeOutSeconds = 30;
request.validatesSecureCertificate = NO;
[request startAsynchronous];
solved it, had to set NSURLRequest http method and header like
[request setHTTPMethod:#"GET"];
[request setValue:#"HEADER_VALUE" forHTTPHeaderField:#"HEADE_RNAME_"];

How to post xml data in url as request?

I am developing an application in which i want to post
xml data as request but i am not able to post it correctly ,i think.
My request xml data is
<loginRequest><username>101</username></loginRequest>
and my request is as follows :
`NSString *post=#"<loginRequest><username>101</username></loginRequest>";
NSURL *url = [NSURL URLWithString:#"my url"];
__block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setRequestMethod:#"POST"];
NSMutableData *mutData=[[NSMutableData alloc]init];
[request addRequestHeader:#"Content-Type" value:#"text/xml"];
[request setPostValue:#"test" forKey:#"body"];
[request setCompletionBlock:^{
NSData *data=[request responseData];
NSString *response=[request responseString];
}];
[request setFailedBlock:^{
NSLog(#"Failed");
}];
[request startAsynchronous];
`
Kindly help me with this..
You can check with your server api whether it supports different response type..
Accordingly you can set "Accept" parameter of HTTP request header.
[request addRequestHeader:#"Accept" value:#"application/xml"];

Set different callback with ASIHTTPRequest

For an app i'm currently making i use the ASIHTTPRequest API to do my communication:
NSURL *url = [NSURL URLWithString:#"http://testService.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request appendPostData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
[request addRequestHeader:#"Content-Type" value:#"application/json"];
[request setTimeOutSeconds:20.0f];
[request setRequestMethod:#"POST"];
NSData * postData = [NSJSONSerialization dataWithJSONObject:dictionnary2 options:0 error:nil];
[request setPostLength:[postData length]];
[request appendPostData:postData];
[request setDelegate:self];
[request startAsynchronous];
i already have working calls in place but they both go to the same callback method :
- (void)requestFinished:(ASIHTTPRequest *)request
I want each call to have it's own callback method since i call one call from the callback method of the other. How can i do this ?
In this kind for situation i would always prefer to use Blocks for call backs.
Check this link for block implementation methods and design,

JSON touch iphone-sdk, send data over request

people,
I'm using JSON touch inmy iphone app.
Now I have to send a string and then an array to server, how can I do this?
I get data from json requests succefully, but I have to send some data.
Here is the code I've got so far:
-(void)subimtSelection:(int)aNumber
{
NSString *choiceData=[NSString stringWithFormat:#"%d", aNumber];
NSError *theError=nil;
[[CJSONSerializer serializer] serializeString:choiceData error:&theError];
NSDictionary *jsDic=[NSDictionary dictionaryWithObject:choiceData
forKey:#"selection"];
//WHAT SHOULD I DO NEXT?
}
You can use ASIHTTRequest to POST string/json data to server:
ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:#"http://server.url"];
[request addRequestHeader:#"Accept" value:#"application/json"];
[request addRequestHeader:#"Content-Type" value:#"application/json"];
[request setRequestMethod:#"POST"];
[request appendPostData:[yourJSONString dataUsingEncoding:NSUTF8StringEncoding]];
[request startSynchronous];
If you want to post a string value then try:
[request appendPostData:#"key=value"];
ASIHTTPRequest can be used in asynchronious mode as well.
P.S. I did not tested the code, but it should work.
To post form data you can use ASIFormDataRequest, documentation is here

ASIFormDataRequest POST returning website source code?

I am using the following code to set the username and password to a form on a website:
ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:url];
[request setRequestMethod:#"POST"];
[request setPostValue:[[NSUserDefaults standardUserDefaults] objectForKey:kUsername] forKey:#"username"];
[request setPostValue:[[NSUserDefaults standardUserDefaults] objectForKey:kPassword] forKey:#"password"];
[request setTimeOutSeconds:40];
[request setDelegate:self];
[request startAsynchronous];
However I am NSLogging the responseString from this request, and it is just printing out the source code of the website rather than any information.
This is likely a server-side issue or a logic error. In addition to responseString, look at responseHeaders and responseStatusCode to make sure you're getting what you expect.