title: Gradle build: Server returned HTTP response code: 401 for URL https using artifactory
body: <p>In the build.gradle of a module, I'm trying to execute the following line</p>
  <pre><code>if (!jsonFile.exists()) {
      new URL(mapUrl).withInputStream{ i -&gt; jsonFile.withOutputStream{ it &lt;&lt; i }}
  }
  </code></pre>
  <p>The build fails with the error:</p>
  <pre><code>Server returned HTTP response code: 401 for URL: https://artifactory.myurl.com/artifactory/myfile.json
  </code></pre>
  <p>I pass the credentials to my gradle script as follows:</p>
  <pre><code>buildscript {
      repositories {
          google()
          jcenter()
  
          maven {
              url 'https://artifactory.myurl.com/artifactory/'
              credentials {
                  username = &quot;myuser&quot;
                  password = &quot;mypwd&quot;
              }
          }
      }
  }
  </code></pre>