ASIFormDataRequest POST returning website source code? - iphone

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.

Related

What are the working of ASIFormDataRequest and ASINetworkQueue in Iphone

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:#"http://trade2rise.com/project/dry/windex.php?itfpage=register"]];
ASINetworkQueue *networkQueue = [[ASINetworkQueue alloc] init];
[request setPostValue:txt_name.text forKey:#"name"];
[request setPostValue:txt_con_number.text forKey:#"phone"];
[request setPostValue:txt_email.text forKey:#"email"];
[request setPostValue:txt_pwd.text forKey:#"password"];
[request setPostValue:txt_con_pwd.text forKey:#"password2"];
[request setDelegate:self];
[request setDidFinishSelector:#selector(requestFinished:)];
[request setDidFailSelector:#selector(requestFailed:)];
[networkQueue addOperation:request];
[networkQueue go];
I am new for the Iphone Please explain about abodve term?
If you known the http post method , you will known the key and value pair at the post form. so the code above just like add relative key and value to the form, the add the queue to post request.
ASIFormDataRequest *request // the url bind request,just form post form
ASINetworkQueue *networkQueue // the work queue to maintain the request in a queue (FIFO)

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

Log into website with ASIHttpRequest

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.

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

asihttprequest: post problems on the iphone

I am trying to to establish a connection between my app and an internet service and I am using asihttprequest but I'm having a small problem. Everything works great when I am on WiFi but when I turn it off and use GPRS(EDGE) or 3G nothing seems to work. Should I change something.
Here is some of my code
[self setRequest:[ASIFormDataRequest requestWithURL:[NSURL URLWithString:#"example.url.php"]]];
[request setPostValue:textString forKey:#"mytext"];
[request setData:imageData withFileName:theFinal andContentType:#"image/png" forKey:#"userfile"];
[request setPostValue:textString2 forKey:#"description"];
[request setPostValue:latitude forKey:#"latitude"];
[request setPostValue:longitude forKey:#"longitude"];
[request setPostValue:finalIdString forKey:#"city_id"];
[request setTimeOutSeconds:60];
[request setUploadProgressDelegate:progressIndicator];
[request setDelegate:self];
[request setDidFailSelector:#selector(uploadFailed:)];
[request setDidFinishSelector:#selector(uploadFinished:)];
[request startAsynchronous];
found it i had to use
[ASIHTTPRequest setShouldThrottleBandwidthForWWAN];