how to pass an string value in url in iPhone app? - ios

I have this URL I want to pass Self.Id in this url cid=self.Id .I am pass following why but getting result invalid request.
NSURL *url = [NSURL URLWithString:
#"http://sms.instatalkcommunications.com/apireq/GetCommentsForSMS?
t=1&h=admin&last=0&cid=Self.Id&items=5"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setRequestMethod:#"GET"];
[request setDidFailSelector:#selector(requestCompleted:)];
[request setDelegate:self];
[request startAsynchronous];

[NSURL URLWithString:[NSString stringWithFormat:#"http://sms.instatalkcommunications.com/apireq/GetCommentsForSMS?
t=1&h=admin&last=0&cid=%#&items=5",Self.Id]];

Related

Launching web url with Session

I am developing HTML web page. I am creating session id in that.
I want to use the same session id for opening that web site through my iOS app as well.
NSString *urlString = #"http://www.myserver.com:8080/Test/TestPage";
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webview loadRequest:urlRequest];
Is it possible to launch a web url using Session ID appended in iOS?
If I am not understanding it in the wrong way, you just want to add some parameters to your request, in this case sessionID. In this case you should do this:
NSURL *url = [NSURL URLWithString: #"http://www.myserver.com:8080/Test/TestPage"];
NSString *body = [NSString stringWithFormat: #"sessionID=%#", #"val1"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url];
[request setHTTPMethod: #"POST"];
[request setHTTPBody: [body dataUsingEncoding: NSUTF8StringEncoding]];
[webView loadRequest: request];
On the other way around, you can use for example AFNetworking Library that will allow you to do things like this:
NSDictionary * parameters = #{#"sessionID" : #"123"};
NSMutableURLRequest * mutableRequest = [self requestWithMethod:#"POST"
path:#"http://www.myserver.com:8080/Test/TestPage"
parameters:parameters];
Anyways, #Popeye is right, you would need to manage this somehow in your server.

ASIHttpRequest not passing data

NSURL *url = [NSURL URLWithString:#"http://test.com/misc/app/ws/ws_user_registration.php?"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setRequestMethod:#"POST"];
[request setDelegate:self];
[request appendPostData:[[NSString stringWithFormat:#"{\"user_name\":\"%#\",\"user_tag_status\":\"%#\",\"description\":\"%#\",\"mobile_number\":\"%#\",\"device_type\":\"%#\",\"device_id\":\"%#\",\"device_token\":\"%#\",\"os\":\"%#\",\"os_version\":\"%#\",\"image_data\":\"%#\",\"user_id\":\"%#\"}",self.phnNumTxtFld.text,#"live",#"description",self.phnNumTxtFld.text,#"iPhone",appDelegate.deviceID,appDelegate.deviceToken,#"iOS",[UIDevice currentDevice].systemVersion,[data base64EncodedString],#""] dataUsingEncoding:NSUTF8StringEncoding]];
[request setDidFinishSelector:#selector(getTreeRequestFinished:)];
[request setDidFailSelector:#selector(getTreeRequestFailed:)];
[request startAsynchronous];
appendPostData: passing NSData but server not getting this data.
To post data use ASIFormDataRequest
NSURL *url = [NSURL URLWithString:#"http://test.com/misc/app/ws/ws_user_registration.php?"];
ASIFormDataRequest* asiRequest = [ASIFormDataRequest requestWithURL: url];
[asiRequest setPostFormat:ASIMultipartFormDataPostFormat];
[asiRequest addData:DATA_TO_SEND withFileName:FILE_NAME andContentType:FILE_CONTENT_TYPE forKey:YOUR_FILE_UPLOADKEY;
[asiRequest setDidFinishSelector:#selector(connectionFinishedLoading:)];// called when connection successfully got finished and recieve resoponse
[asiRequest setDidFailSelector:#selector(requestWentWrong:)]; // called when connection went wrong due to bad url connection or timeout, etc.
[asiRequest setDelegate:self];
[asiRequest startAsynchronous];
For more info read asihttp docs

How to send data in url to server?

http://novatoresols.com/demos/blow/users/add.json?json={"email":"ali"}
How can I send a key and data to web? Here "email" is the key and "ali" is the value in URL.
Code:
NSURL *url=[NSURL URLWithString:[hostURL stringByAppendingFormat:#"users/add.json?json="]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSString * params =#"{\"email\":\"Ali\"}";
[request setHTTPMethod:#"POST"];
// This is how we set header fields
[request setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
Simply Try with this :
NSString * params =#"{\"email\":\"Ali\"}";
NSURL *url=[NSURL URLWithString:[hostURL stringByAppendingFormat:#"users/add.json?json=%#",params]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog (#"%#",data);

ASIHttpRequest: Can someone see the URL being sent?

I'm using ASIHttpRequest to send data back and forth to the server in my IOS game.
I'm wondering if someone can see the url being send to the server, someway?
... If that's the case, cheating is possible.
thanks in advance
The request looks like this:
NSURL *url = [NSURL URLWithString: #"http://xx.xxx.xx.x/development/"];
__block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL: url];
__weak ASIHTTPRequest *request_b = request;
[request setDelegate: self];
[request setQueue: [[DatabaseHelper sharedInstance] networkQue]];
[request setShouldContinueWhenAppEntersBackground: NO];
[request addRequestHeader:#"Content-Type" value:#"text/html; charset=utf-8;"];
[request setDefaultResponseEncoding:NSUTF8StringEncoding];
[request setTimeOutSeconds: 20.0f];
[request setCachePolicy: ASIDoNotWriteToCacheCachePolicy | ASIDoNotReadFromCacheCachePolicy];
[request setPostValue:#"aSecretString" forKey:#"secret"];
[request setPostValue: #"updateUser" forKey:#"apiToRun"];
[request setPostValue: #"4" forKey:#"userId"];
url:
http://xx.xxx.xx.x/development/secret=aSecretString&apiToRun=updateUser&userId=4
With a GET request, data is sent as part of the URL and is easy to intercept with HTTP monitoring. If you use POST, this information will not be visible in the URL.

NSMutableURLRequest transform to ASIFormDataRequest

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.

Resources