how to extract the url path from my url link in Xcode? - iphone

I have a URL link like "http://mobile/testing.php?action=2, when i have this link on my IE browser, it will redirect the link to "http://mobile/myimage.jpeg". It directed to my database server folder name, where my the file I stored called myimage.jpeg.
I am wondering I how can I can put "http://mobile/testing.php?action=2", establish the NSURL connection and let the Xcode learn this path "http://mobile/myimage.jpeg" so that I can extract the image.jpeg and store it into a list where this list can reflect all the files I had in my database.
Anyone can help? would there be any NSURL reference I can use? I did checkup on the pathomponent, but I don know how can I implement it.

If you want to know where it actually gets redirected to, then you'll need to use an asynchronous NSURLConnection and implement the -[<NSURLConnectionDelegate> connection:didReceiveResponse:] delegate method.
The NSURLResponse object passed to that method will actually be an NSHTTPURLResponse, which means you can:
Ask it for its -statusCode. If the code is a redirection code, then you know your original request was redirected, at which point you can...
Ask it for its -URL. This is the NSURL to which your original request was redirected. From here, you can get its -lastPathComponent to extract the "myimage.jpeg" bit.
If I've totally misunderstood your question, then you can probably just extract the -path from your starting URL and get the -lastPathComponent from that.

Related

URL links in email - resolve correct url?

Before i start thinking about this programatically, does anyone know if it is possible to actually extract the correct url from an email link that is basically a tracking module?
Our work email system auto blocks tracking based urls from email, so i am thinking of writing something to extract the correct url so people can copy and paste the tracking link into a program and it will provide the correct url.
Is this even possible with the way that email tracking works?
Here is an example of a url in an email that i recently received:
http://t.dripemail2.com/c/eyJhY2NvdW50X2lkIjoiNTE0MTQ4NSIsImRlbGl2ZXJ5X2lkIjoiOTI0NzI2MTU0IiwidXJsIjoiaHR0cHM6Ly93d3cuYXhzaWVkLmNvbS9nY3NlLWNvbXB1dGVyLXNjaWVuY2Uvb2NyLW5lYS1ndWlkZS8_X19zPXphb2txcDVpaWN4NGkxZndtYmNnIn0
Our system blocks these. It eventually resolves to:
https://www.axsied.com/gcse-computer-science/ocr-nea-guide/?__s=zaokqp5iicx4i1fwmbcg
(got our network admin to check it for me)
I want a system that gets the right url from the ugly mess that is blocked so we can actually view links from emails.
Thanks in advance for any help.
The data in tracking URLs are typically a unique ID pointing to some entry in a database, or are encrypted with a private key, so there's no way to obtain any meaningful information from them. (see answers to this related question: Generate unique link for each website visitor)
More naive approaches will simply encode the data, in which case you may be able to extract useful information from them. Funnily enough, your example URL is a base 64 encoded JSON object containing the link itself:
{
"account_id": "5141485",
"delivery_id": "924726154",
"url": "https://www.axsied.com/gcse-computer-science/ocr-nea-guide/?__s=zaokqp5iicx4i1fwmbcg"
}
In this case you could actually resolve the URL on your own, but this type of approach is uncommon for that very reason.

Programmatically downloading image from CQ5

I am trying to programmatically download a image from CQ5.I have made a link which when clicked should download a image.To do this I have made a ajax call to a servlet ,for whenever user clicks on the image,we should get a pop of open and save dialog.
This is the code I have written in servlet to download the image.
**response.setContentType("image/png");
response.setHeader("Content-Disposition", "attachment; filename=icon" + ".png");
URL url = new URL("http://somehost:portnmuber/content/dam/image.jpg");
URLConnection connection = url.openConnection();
InputStream stream = connection.getInputStream();**
I am continuously getting 401 error.
Is there some another way that I can do this.
Thanks,in advance.
Firstly, have a look at the download component in the foundation libs:
/libs/foundation/components/download/download.jsp
Secondly, if you don't have a session open with CQ5, it will by default give you a 401 error. I am assuming you are in a generated container for CQ5, or are you just randomly accessing from another domain?
Note that even for anonymous access, CQ will still establish an anonymous session, and give you a login token. Anonymous is effectively still authenticated, just without the requirement of a username and password.
You are gettin 401 error because anonymous user permissions for the DAM are not set.
At first, you should grant access permissions for 'anonymous' users for node '/content/dam'.
You can do it from the page: "http://somehost:portnuber/useradmin" . Doble click on user 'anonymous' and go to the tab 'permissions' (on the window right). Check read permissions for 'dam' node.
Secondly, go to the system console, components tab (http://somehost:portnmuber/system/console/components) and find org.apache.sling.engine.impl.auth.SlingAuthenticator component. Click to 'configure' button. In the component configuration check 'Allow Anonymous Access' and press 'Save'.
Should work.
CQ5 uses sessions so downloading from a path requires a username/password on the author instance, for example curl -u username:password http://someserver.com:4502/etc/packages/my_packages/package.zip -o localpath/package.zip would download a package based on someone who had permissions to read it. For images you need the image path something like http://someserver.com:4502/content/dam/640x960.jpg to get the image, but you would need to add /jcr:content/renditions/original to get the original e.g. http://someserver.com:4502/content/dam/640x960.jpg/jcr:content/renditions/original. You can also change the image path to get a specific rendition of the image, for example http://someserver.com:4502/content/dam/640x960.jpg/jcr:content/renditions/cq5dam.thumbnail.140.100.png gets a 140x100 version of the image.
I had a similar issue trying to download images from AEM to iOS devices. It turns out AEM (at least the instance I'm hitting) requires a referer header; if absent the request is rejected. The problem did not surface with the simulator, just with real devices. This did the trick:
[request setValue: #"https://www.example.com/" forHTTPHeaderField: #"Referer"];

POST request from iPhone to Django

I'm trying to send a POST request from iPhone to Django, but for some reason I can't. The request is sent, but the server doesn't receive it properly.
What is the regular expression that accepts POST? In my case, I use this one: /messages/a/s=person1&r=person2&c=hello/.
How do I retrieve the POST arguments in the Django view? request.POST['s'] should work?
Thanks.
POST parameters are not part of the URL, so your regex should simply detail the main part of the url you want to receive it on. To take your example, change it to /messages/a/. Then, in your "messages" app, have a view/function called a: that one will be reached on receiving any POST (or GET, which you're currently (almost) depicting in your url) to that location.
The arguments can then indeed be retrieved using request.POST['keyname']. To make things more convenient, supply a default value when getting the data so you need less error checking: request.POST.get('keyname', None). This will get the value of keyname when available, or None otherwise.
The posting itself... depends on more code then you're currently showing. Can't say anything about that with your current question.
That URL you've pasted in will pass the data through the request.GET dictionary. If you want to change your iPhone app to POST data, you'll have to share your code.

How to know route from the URL or $request Object?

PLOT:
After implementing ACL on my website, if a user tries to access unauthorised page he will be denied and shown a page to login. After he loggs in, I wanted to redirect the user to the previous page to which he was denied to earlier.
To do this, I store the request parameters using $request -> getParams(), onto a session variable, which will be used to generate the url again. This is where the problem occurs, to generate the url back, I need the name of the route and that i dont know how to read.
I need to know the route name, so that I will be able to regenerate the url, from the array stored in session, or if there is a better way to solve this, please suggest.
Why not just store $request->getRequestUri()? This will give you the URL as it appears in the browser.
Dont try to think of complex solutions for simple problem.
You can do this, with just using $_SERVER['REQUEST_URI'], this gives the same result as #Phil's answer (Correct me, If i am missing something). and is more than enough to do what you want.

iPhone: How do I securely transfer data from device to server

could someone please assist me, Im a newbie and haven't done this before.
I have an iPhone app which has a "cart" object. Once the user has made his selections, I want to send that info in an XML file to the server. I read that the iPhone libraries let you convert data into XML easily. But from what I understand the data has to be stored in an array or a dictionary. Is this true? For example, my "Cart" is an object that uses a array to store data internally but the "cart" itself has variables which are not stored in an array or dictionary.
Q) How can I convert the entire "cart" into something that can be transported as XML to my server?
Also, I am asking the customers to create a profile for each order but this will be done by Launching UIWebview directly on the web through an https connection. So no credit card info will be on the device.
Q) Whats the best way to link the profile on the web and the order which is on the device?
Could someone who has come across this issue please give me tips or links?
Thanks
Sending your data securely could be done with ssl.
Converting your cart to XML could be done with an XML Framework/lib, still you'll have to write the code for that. For example touchXML:
TouchXML is a lightweight replacement
for Cocoa's NSXML* cluster of classes.
It is based on the commonly available
Open Source libxml2 library.
Here is a nice tutorial.
Source has moved a bit, can be found here
To your second question: This is tricky and may get hacky. However if you don't want to switch to an API based way to login/create the account I'll have these ideas.
Check the "result" of webView with the UIWebViewDelegate protocol with the webViewDidFinishLoad: method. A "result" may be: successful creation of an account or successful login.
So you can access the body of the page with the NSURLRequest property of the webview. Or use something like this, using javascript:
NSString *html = [webView stringByEvaluatingJavaScriptFromString: #"document.body.innerHTML"];
You will have to do parsing though to look for something, for example a token which you can connect with the order for final checkout.
You also could have generated a unique order string on the device in the first place and sent it over initially for login/creation of profile to increase security a bit and pass it back for a check.
Part one: create a dictionary that represents the cart. For each variable, add a key with its variable name as the key, and the cart object's value for that variable as the object for that key.
Part two: that's a very open-ended question :-). That depends on how you identify the user at both ends; though of course that will depend on your security requirements. One option is to require your user logs in via the web site when they first launch the device, then store their user ID on the app on a particular device (preferably in a confidential fashion). Don't use that for any reason other than to track which user you think is at the device: authenticate again before each purchase, or other sensitive actions like viewing or changing account details. By the way depending on the way your ordering system works, you may prefer (or be required by Apple) to implement in-app purchase. That would actually take a lot of the complexity away from the problem, at the cost of Apple's 30% processing fee.
If you have specific questions about the security concerns of such a system, you would do well asking at security.stackexchange.com (I'm one of the pro tem moderators over there).
If you send an encrypted string to a URL like in the code below, then handle it in an ASPX page (.NET example) or you can use other languages on the server side. Then, for security, simply ignore anything that doesn't decrypt (basic encryption-decryption not shown)
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL
URLWithString:#"http://myzuresite.azurewebsites.net/test3.aspx?bigstring=dgrbthymgk"]];
[request setHTTPMethod:#"POST"];
[NSURLConnection connectionWithRequest:request delegate:self];}
Then, in the http://myazuresite.azurewebsites.net/test3.aspx page:
<%# Page Language="C#"%>
<html>
<head>
<title>Query Strings in ASP.NET: Page 2</title>
<script language="C#" runat="server">
</script>
</head>
<body>
<%
// retrieves query string values
string bigstring = Page.Request.QueryString["bigstring"];
System.Data.SqlClient.SqlConnection sqlConnection1 =
new System.Data.SqlClient.SqlConnection("Data Source=myazuresite.database.windows.net;Initial Catalog=db_name;Integrated Security=False;Persist Security info=False;User ID=your_id;Password=your_Password”);
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = "INSERT Scores (col1) VALUES (' "+name+" ')";
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
cmd.ExecuteNonQuery();
sqlConnection1.Close();
%>
</body>
</html>
I handle the decryption in an insert trigger in SQL Server, ignore bad URL's, and this is VERY secure. I use encryption that includes time in some way.