10

I send the same HTTP message from a HTTP proxy client and with NSURLRequest + NSURLConnection, and get back different result. It is an authentication request. From HTTP proxy authentication request is accepted, sending from app not. Why? Accepted means after redirection HTML will contains no Oops substring.

enter image description here

let url = NSURL(string: "http://www.swisshttp.weact.ch/en/user/login")
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")

let email2 = (viewController!.email.text as NSString).stringByReplacingOccurrencesOfString("@", withString: "%40")
let str = "name=\(email2)&pass=\(viewController!.password.text)&form_id=user_login" as NSString
let d = str.dataUsingEncoding(NSUTF8StringEncoding)
if let d2 = d {
    request.HTTPBody = d2
    let urlConnection = NSURLConnection(request: request, delegate: self)
}

UPDATE

I have put @teamnorge's code below into playground and into an empty Single View Application project. Returned HTML in project contains the Oops substring, code used in playground not containes it, any idea what is going on, why same request produce different HTML result? I get failed message also from iOS device and from simulator too.

UPDATE

Removed NSURLRequest cache like here recommended, but still not works as expected. And here.

UPDATE

Tried to remove all the credentials like here, but didn't help, no credential was found.

Community
  • 1
  • 1
János
  • 32,867
  • 38
  • 193
  • 353

1 Answers1

2

It looks like when you receive HTTP 302 and new Location URL, iOS does automatically fetch the page by this URL, so I guess your response is in fact the HTML content of the redirection page. Please verify.

UPDATE:

import UIKit
import XCPlayground

let url = NSURL(string: "http://www.swisshttp.weact.ch/en/user/login")
let request = NSMutableURLRequest(URL: url!)
let str = "name=kukodajanos%40icloud.com&pass=jelszo&form_id=user_login" as  NSString
let d = str.dataUsingEncoding(NSUTF8StringEncoding)
request.HTTPBody = d
request.HTTPMethod = "POST"
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")

NSURLConnection.sendAsynchronousRequest(request, queue:     NSOperationQueue.currentQueue()) { response, maybeData, error in
   if let data = maybeData {
       let contents = NSString(data:data, encoding:NSUTF8StringEncoding)
       println(contents)

            if contents!.rangeOfString("Oops").length == 0 {

                println("success")
            } else {
                println("failed")
            }
   } else {
       println(error.localizedDescription)
   }
}

XCPSetExecutionShouldContinueIndefinitely()
János
  • 32,867
  • 38
  • 193
  • 353
teamnorge
  • 784
  • 5
  • 9
  • partly you are right, iOS automatically redirects request, but the resulted page is not the same what I get in proxy client, iOS will return with failed authentication message, proxy with success message – János Jul 23 '15 at 13:19
  • I took the liberty to use your credentials and post a data from my playground, the only difference is that web login redirects to /challenge and ios login redirects to /user the rest seems to be ok, see my code in the post above. – teamnorge Jul 23 '15 at 13:44
  • used your code, and found a really weird effect, in playground it returns `success` but in app it returns `failed`, any idea why? – János Jul 28 '15 at 12:47
  • @János, it's quite strange, but then you may change the `queue` you are going to run your url request on from `current` to `main`. `NSOperationQueue.mainQueue()` and add `dispatch_main()` to your main function, do you see any differences? It's not a real answer, just to comprehend what's wrong to find a solution. – teamnorge Jul 28 '15 at 13:08