Sending HTTP Requests in Swift - http

I have tried all the methods I came across on Google but none work. So my question is how would I get the contents of a web page using Swift? Like send an HTTP Request to a website and get the source of the page.
Any help or references welcome :D.
I have tried Alamofire and a bunch of other projects on Github as well.

This code will get you the source code of a website in text form:
let source = NSString(contentsOfURL: NSURL(string: "http://www.google.com"), encoding: NSASCIIStringEncoding, error: nil)
Edit: If the website contains non-ascii characters, you can of course use 'NSUTF8StringEncoding'. If you don't want to block the main thread you should of course use an async_dispatch call.

How about NSURLConnection and NSURLRequest?
sendAsynchronousRequest:... passes an NSData object into the completion handler alongside the NSURLRequest. sendSynchronousRequest:... (please avoid) returns an NSData object. Starting a request with start requires you to have hooked up a delegate to that request that implements NSURLConnectionDelegate where various methods are called as data comes in or the request fails. I'd recommend sendAsynchronousRequest:....

Related

Logging into a webpage via HTTP Request

So I have a webpage, ("http://data.terapeak.com/verify/") and I don't see any & tags in the URL so I am unaware how to post data to this. I need to do this via HTTPRequest rather than browser control. I am creating a double threaded batch searching program. I have already successfully made this using a single browser control but that wont allow for multi-threading, atleast with my current knowledge due to the fact that even when creating a new frmBrw that already exists it needs for me to set the threat apartment to single. If i set it to single, I am unable to have it send the data the the excel sheet I need both threads to access. I hope this is clear... The basic question is how can I log into this form via HTTP request.
This isn't going to be easy to answer without further details however I suspect you'll need to provide the variables via a HTTP POST request.
Can you successfully login to this page in your browser? If so, run a proxy tool such as fiddler and check the HTTP headers it makes to the server. You should see the form variables being passed over. You then need to mimic this in code.
How to: Send Data Using the WebRequest Class
Hope this gets you started

NodeJS Express DynamicView Helper with an http request

I hava a node.js app running on express with a dynamicViewHelper that hits an http service with http.request. Unfortunately, when I try to do this, because the http request is asynchronous and the function calling it is not, the page just renders before the http request is finished and I end up with undefined on my page.
I am basically looking for a way to put an asynchronous action (the http request), inside of a dynamic view helper which gets rendered out on the page. I know I can't just turn it into a synchronous function, so I am looking for another way to get that dynamic data to the view.
Also, is there a way to cache the data that is returned from an http.request()? I don't want to put it on the session, but I want any further requests made to the exact same URL to possibly be cached... Not as big of a deal as the first part of my question, though.
I figured it out. I'll just load it in my route so that res.render() isn't called until the request is done.

how to create JSON webservice, can Anyone refer any tutorial for this

I Cant Able to find out correct soltion . Our client is Android and Iphone. now i need to create a webservice for these two client. I dont know even a single step about this. please any one refer any tutorial website or guide to me.I am using .asmx form.And i use C#
At a high level, its very simple. Instead of returning HTML code in response to an HTTP GET or POST, you will now return a JSON string. The real question is how do you create a JSON string from the object that you want to send.
There are several libraries that can take an object and return a JSON string. In python you can use
return simplejson.dumps(<<object name>>)
in php,
echo json_encode(<<object name>>);
Which language are you using?
I'd recommend to do a REST service.
I've accomplished that by using an HTTP Handler (.ashx) and NewntonSoft.Json (just returning the literal string and changing the content type to "application/json".
If you want to be more fancy WCF supports json as well but I found the handler approach to work very well. Also the Json serializer that Newton uses worked better for me than the Microsoft one.
You can get his Json library using Nuget as well as downloading from the site.
Some resources:
http://www.dotnetperls.com/ashx and
http://james.newtonking.com/pages/json-net.aspx

http post from firefox extension to ASP.NET

I want to be able to send a simple http post from my firefox extension to my ASP.NET application. From the client side, I have used XMLHTTPRequest by sending the post with the url: http://localhost:15227 which is the url on my ASP.NET app.
What do I need to do to receive a remote request from ASP.NET please?
thanks
This page from Apple has a pretty good example of how to send/receive data using a raw XmlHttpReqeust object (as opposed to a JavaScript library).
You can get the value of the response from the responseText property once you know the response came back successfully. Specifically take a look at where the processReqChange function is defined, your code will go in there (or your equivalent of that function).
If you want to explore JavaScript frameworks, take a look at how much less code you need if you use jQuery get (for example).

AJAX call works on local machine, not on host

What could potentially stop an AJAX call from working on the host server, when it works fine on the local host? I tried returning an error from the AJAX call, but all I get is 'undefined'. I don't think the actual page method is being called since no information is added in my log (and I've explicitly added a call). I can't reproduce the problem on my local machine, so does anyone know possible areas I should look into?
Thanks
What URL is in your Browser, and What URL is your Ajax call hitting? Browsers have 'security' constraints that don't allow cross domain AJAX calls. So for instance, if I am loading a local file:
http://localhost:20080/myCustomPage.html
and within that page, I make an ajax call to
http://search.twitter.com/search?q=test
I would get a security warning from the browser. Depending on your browser this may manifest itself in a warning icon in the bottom corner (IE), or an error in the javascript log (firefox).
There is a way to get around this in ONE specific case. That case being any GET request. To do this instead of making an 'ajax' call you include a tag. That script tag will then be read and loaded. The catch here, is the call now needs to include a "callback" method, that you can implement on your side, that gets called with the result of the call.
So instead of a response like:
{
"first_name": "peter",
"last_name": "parker"
}
you would need to return
myCallBackFunction({
"first_name": "peter",
"last_name": "parker"
});
This example is using JSON, but you could easily use XML, HTML or any other result format as long as the function is called.
myCallbackFunction("INSERT RESPONSE TEXT HERE")
This method is commonly refereed to as JSONP and is fortunately implemented in the common javascript libraries like jquery from the client perspective. If you control the server side, you will need to hard code a callBackFunction wrapper, or expose a parameter that allows the client to set it. And unfortunately if you don't own the library there isn't much you can do unless the owner of the service already provides that feature. Fortunately most Web 2.0 services you would be doing stuff like this, already implement that feature.
I've managed to fix it. There was an issue with running IIS7 in integrated mode. I moved all my modules from to and my to (also in the section)
Hope that helps anyone else.
More info

Resources