Get cookie from NSURLRequest to WebView - iphone

The goal of my program is to do the following:
Execute a url on my website, which 'logs' the user in using NSURLRequest. Basically my thought is to execute the request, get the cookie from the response and add the cookie to the WebView.
All I know how to do is execute the first part of this process, and the code is below for that:
NSString *fullURL = #"http://mysite.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
content is the name of my webview
[content loadRequest:theRequest];
Next up is parsing the response. Basically check for error (the page only outputs the word 'true' or 'false' for error. I'm guessing I can check for the existence of those words and act appropriately. And of course, getting a cookie from the response and sending it to 'content'. Would appreciate help, thanks!

Normally URL loading system automatically handles sending and storing all URL related cookies with your NSURLRequest. However if you want to add an extra cookie, you must use NSMutableURLRequest.
Cookies are part of request response headers, which you can modify like this:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request addValue:cookie forHTTPHeaderField:"Cookie"];

Related

GET and POST requests from ios5

I'm working on making a client for my REST service on the iPhone. I'm a little lost as to how I go about making the GET and POST requests. I make the url from a NSString, convert it to an NSURL and create the NSURLRequest based off of the url. After that I'm pretty lost. Also, sometimes I care about the response, other times I don't. For example, when making a request for a new id, I care about the response because it's the id I'll use to upload my file later, but when I upload the file I don't care because the server doesn't send a response.


Does anyone have some (hopefully)simple sample code that they could point me to / share?

What I have so far:
-(NSString *) makeGetRequest:(NSString *)url :(Boolean)careAboutResult
{
NSString *results = nil;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
NSError *reqError;
NSURLResponse *response = nil;
if(careAboutResult == YES)
{
//get the result
}
return results;
}

In the code I'm testing with, the URL is
http://192.168.0.108:8081/TestUploadService/RestfulUpload.svc/id/test123_DOT_png
and I'm saying I do care about the result.

Any help would be greatly appreciated.
#nick its good you have created a NSURLRequest now you just need to create a connection to send this request and receive response, this request is GET request.
To make POST request you will need to use NSMutableURLRequest and set its method name and body content. Here in documentation you will find how you can do this.

how to open url need username and password with uiwebview?

I want to open an URL which need password and username in a UIWebview. Such as open my local Wifi Router(192.168.1.1). But when I try following code, there is no popup as Safari to require password and username.
NSURL *url = [NSURL URLWithString:#"http://192.168.1.1"];
NSURLRequest *httpReq = [NSURLRequest requestWithURL:url];
[self._webView loadRequest:httpReq];
Since someone told me to use NSURLConnectionDelegate, I know this, but I donot know how to show the authorized page to the UIWebView.
This will help
NSURL *url = [NSURL URLWithString:#"http://username:password#192.168.1.1"];
NSURLRequest *httpReq = [NSURLRequest requestWithURL:url];
[self._webView loadRequest:httpReq];
You need to implement connection:didReceiveAuthenticationChallenge: in NSURLConnectionDelegate. For details, read Authentication Challenges chapter from Apple "URL Loading System Programming Guide".
UIWebView doesn't provide any mechanism to identify the response. So one solution is explained in UIWebViewHttpStatusCodeHandling github project which identifies the status code of the response (should be 404 in your case).
However, the main drawback is for each request you need to use NSURLConnection and load the request again. But in this case, you can cancel the NSURLConnection too.
The other solution should (might) be the use of Java-Script. Search for the Java-script, which provides the status code of the response.

Creating HTTPS login on iOS?

How do I build an https login on iOS? (Like this one: https://info.tam.ch/kks)
I only know how to solve a .htaccess.
Thank you.
I use ASIHTTPRequest in my iPhone project to communicate with the web application. For http authentication, they have a convenient ASIAuthenticationDialog class that takes care of that. Basic usage is as follows:
// Specifying a username and password to use in the URL
NSURL *url = [NSURL URLWithString:#"http://username:password#allseeing-i.com/top_secret/"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
// Setting a username and password to use on the request
NSURL *url = [NSURL URLWithString:#"http://allseeing-i.com/top_secret/"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setUsername:#"username"];
[request setPassword:#"password"];
You can visit here for detailed information or check out the sample source code here on github.

How can I send request and get a file response in iPhone?

My problem is the following...
I read about sending http requests and receiving their responses on iPhone SDK 3.2 using NSURLRequest and NSHTTPURLResponse (All my requests are "get" and there's no "post") but I don't know how to do that exactly cause some of my responses are just strings (plain text) and some others are binary files (gzip and mp3)
Thank you in advance for the help
To perform a Http request, I use ASIHttpRequest:
NSURL *url = [NSURL URLWithString:my_url];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setRequestMethod:#"GET"];
[request startSynchronous];
That works perfectly. Still need to figure out if it's fine with mp3 / zip files.
Luc

iPhone: Post data to php page

Despite looking at similar posts on this site and google, I just can't wrap my head around how to post to a php page from the iPhone. Here is what I want to do:
I have a php script, say at www.mypage.com/myscript.php that I could post to normally by doing www.mypage.com/myscript.php?mynumber=99&myname=codezy
This in turn will add a log message in database for example. I do not want any data back, it is essentially a one way transaction. NSMutableURLRequest seems to be what I am looking for but I just cant get a handle on how to make this work with a couple parameters.
If you're sending a single URL request without needing to send multiple variations, NSURLRequest will do. Even though you are using multiple parameters, they are all part of the same URL, so you just treat them that way. Build the URL as a string first and then use the string to initialize a NSURL object.
You are prompting a log message on the server, but you will want to have response data in case something goes wrong. You can just ignore the response data unless there's an error. The request is sent using a NSURLConnection object.
NSURL *urlToSend = [[NSURL alloc] initWithString: #"www.mypage.com/myscript.php?mynumber=99&myname=codezy"];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:urlToSend
cachePolicy:NSURLRequestReturnCacheDataElseLoad
cachetimeoutInterval:30];
NSData *urlData;
NSURLResponse *response;
NSError *error;
urlData = [NSURLConnection sendSynchronousRequest:urlRequest
returningResponse:&response
error:&error];