Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSURLRequest produce different result than HTTP proxy client

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.

like image 812
János Avatar asked Jul 23 '15 12:07

János


1 Answers

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()
like image 115
teamnorge Avatar answered Oct 19 '22 11:10

teamnorge