Notification texts go here Contact Us Buy Now!

Get webpage content with URLSession returns 403

Here's the HTML code for the provided issue and its solutions:

Tried this in a unit test and works fine:

      func testExample() throws {
          let exp = expectation(description: "...")
          var request = URLRequest(url: URL(string: "https://dibamovie14.top")!)
          let task = URLSession.shared.dataTaskPublisher(for: request).sink {
              print($0)
              exp.fulfill()
          } receiveValue: { data, response in
              print(response)
          }

          waitForExpectations(timeout: 4) { error in
              guard let error = error else { return }
              print(error.localizedDescription)
          }
      }
      
    

call is using the dataTaskPublisher but you should be able to apply the changes to your async approach. User-Agent should be set by default anyway when using URLSession and "Host" is not required

Addendum: you can set the User-Agent if you need it but "Host" should not be set:

Host - Header Field Mandatory since HTTP/1.1.[17] If the request is generated directly in HTTP/2, it should not be used.[18]

Async Await

      func testExampleAsync() async throws {
          var request = URLRequest(url: URL(string: "https://dibamovie14.top")!)
          let (data, response) = try await URLSession.shared.data(for: request)
          print((response as? HTTPURLResponse)?.statusCode)
          print(String(data: data, encoding: .utf8))
      }
      
    

another answer

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.