Is there any API to set "Proxy-Authorization" header

According to the document:

"the URL Loading System handles various aspects of the HTTP protocol for you (HTTP 1.1 persistent connections, proxies, authentication, and so on). As part of this support, the URL Loading System takes responsibility for certain HTTP headers:

  1. Content-Length
  2. Authorization
  3. Connection
  4. Host
  5. Proxy-Authenticate
  6. Proxy-Authorization
  7. WWW-Authenticate

If you set a value for one of these reserved headers, the system may ignore the value you set, or overwrite it with its own value, or simply not send it. Moreover, the exact behavior may change over time. To avoid confusing problems like this, do not set these headers directly."

Looks like we'd better avoid setting "proxy-authorization" directly: [req setValue:authenticationValue forHTTPHeaderField:@"Proxy-Authorization"]; or [config setHTTPAdditionalHeaders:@{@"Proxy-Authorization": authHeader  } ];, instead, should go to the session delegate.

However, we would have 2 "requests" if we follow the authentication challenge if we have credentials already: https://developer.apple.com/documentation/foundation/url_loading_system/handling_an_authentication_challenge?language=objc.

So is there any API or other safe way to set "Proxy-Authorization" for nsurlsessionconfig at the "beginning" if we got the credential already?

Thanks in advance.

Replies

For CFNetwork, we could set credentials by CFHTTPMessageApplyCredentialDictionary before the request if we got it already.

However, we would have 2 "requests" if we follow the authentication challenge if we have credentials already

Right. And that’s a problem because?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Thanks eskimo. Could we do:

  1. reduce the first "CONNECT" by adding a "Proxy-Authorization" header if we already got the credential?
  2. it's ok for the 2 "CONNECTs" for the 1st request, but how about the following requests? Do we have the same 2 "CONNECTs" for each request to the same proxy server? is there any way in "NSURLSession" to reuse the credential from 1st request for the following request?

If we use CFNetwork, then we can use CFHTTPMessageApplyCredentialDictionary to set the credential which is from the 1st request for the following request to avoid 2 "CONNECTS".