NSURLRequest produce different result than HTTP proxy client
Asked Answered
D

1

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.

Deviation answered 23/7, 2015 at 12:25 Comment(3)
i'd probably remove your email/password from the screenshot if i was youColeman
one might test authentication request, it was on purposeWanwand
thanks your help, issue was different, two different url was called :-(Wanwand
D
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()
Devereux answered 23/7, 2015 at 13:15 Comment(4)
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 messageWanwand
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.Devereux
used your code, and found a really weird effect, in playground it returns success but in app it returns failed, any idea why?Wanwand
@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.Devereux

© 2022 - 2024 — McMap. All rights reserved.