Repository: pytorch
Issue: Cannot use socks5h proxy because of urllib: `urlopen error Remote end closed connection without response`
Labels: triaged,module: vision,module: hub

### 🐛 Describe the bug

```python
# either
torch.hub.load('pytorch/vision', 'resnet50', pretrained=True)
# or 
from torchvision.models.resnet import resnet50
resnet50(pretrained=True)
```

Basically what is happening is that I have set up `http[s]_proxy=socks5h://localhost:1080` environment variable for `urllib` to use it, and it is. The problem is that is not using the `keep-alive` header and the server is prematurely dropping the connection. If I use `requests` module it works just fine. If you do `urllib.request.urlopen("https://github.com/")` it will be the same error. 

```
During handling of the above exception, another exception occurred:

URLError                                  Traceback (most recent call last)
notebook.ipynb Cell 9' in <cell line: 5>()
      1 # model = torch.hub.load('pytorch/vision', 'resnet50', pretrained=True)
      3 from torchvision.models.resnet import resnet50
----> 5 model = resnet50(pretrained=True)

File ~/.pyenv/versions/miniconda3-latest/lib/python3.9/site-packages/torchvision/models/resnet.py:331, in resnet50(pretrained, progress, **kwargs)
    323 def resnet50(pretrained: bool = False, progress: bool = True, **kwargs: Any) -> ResNet:
    324     r"""ResNet-50 model from
    325     `"Deep Residual Learning for Image Recognition" <https://arxiv.org/pdf/1512.03385.pdf>`_.
    326 
   (...)
    329         progress (bool): If True, displays a progress bar of the download to stderr
    330     """
--> 331     return _resnet("resnet50", Bottleneck, [3, 4, 6, 3], pretrained, progress, **kwargs)

File ~/.pyenv/versions/miniconda3-latest/lib/python3.9/site-packages/torchvision/models/resnet.py:296, in _resnet(arch, block, layers, pretrained, progress, **kwargs)
    294 model = ResNet(block, layers, **kwargs)
    295 if pretrained:
--> 296     state_dict = load_state_dict_from_url(model_urls[arch], progress=progress)
    297     model.load_state_dict(state_dict)
    298 return model

File ~/.pyenv/versions/miniconda3-latest/lib/python3.9/site-packages/torch/hub.py:591, in load_state_dict_from_url(url, model_dir, map_location, progress, check_hash, file_name)
    589         r = HASH_REGEX.search(filename)  # r is Optional[Match[str]]
    590         hash_prefix = r.group(1) if r else None
--> 591     download_url_to_file(url, cached_file, hash_prefix, progress=progress)
    593 if _is_legacy_zip_format(cached_file):
    594     return _legacy_zip_load(cached_file, model_dir, map_location)

File ~/.pyenv/versions/miniconda3-latest/lib/python3.9/site-packages/torch/hub.py:457, in download_url_to_file(url, dst, hash_prefix, progress)
    455 file_size = None
    456 req = Request(url, headers={"User-Agent": "torch.hub"})
--> 457 u = urlopen(req)
    458 meta = u.info()
    459 if hasattr(meta, 'getheaders'):

File ~/.pyenv/versions/miniconda3-latest/lib/python3.9/urllib/request.py:214, in urlopen(url, data, timeout, cafile, capath, cadefault, context)
    212 else:
    213     opener = _opener
--> 214 return opener.open(url, data, timeout)

File ~/.pyenv/versions/miniconda3-latest/lib/python3.9/urllib/request.py:517, in OpenerDirector.open(self, fullurl, data, timeout)
    514     req = meth(req)
    516 sys.audit('urllib.Request', req.full_url, req.data, req.headers, req.get_method())
--> 517 response = self._open(req, data)
    519 # post-process response
    520 meth_name = protocol+"_response"

File ~/.pyenv/versions/miniconda3-latest/lib/python3.9/urllib/request.py:534, in OpenerDirector._open(self, req, data)
    531     return result
    533 protocol = req.type
--> 534 result = self._call_chain(self.handle_open, protocol, protocol +
    535                           '_open', req)
    536 if result:
    537     return result

File ~/.pyenv/versions/miniconda3-latest/lib/python3.9/urllib/request.py:494, in OpenerDirector._call_chain(self, chain, kind, meth_name, *args)
    492 for handler in handlers:
    493     func = getattr(handler, meth_name)
--> 494     result = func(*args)
    495     if result is not None:
    496         return result

File ~/.pyenv/versions/miniconda3-latest/lib/python3.9/urllib/request.py:1389, in HTTPSHandler.https_open(self, req)
   1388 def https_open(self, req):
-> 1389     return self.do_open(http.client.HTTPSConnection, req,
   1390         context=self._context, check_hostname=self._check_hostname)

File ~/.pyenv/versions/miniconda3-latest/lib/python3.9/urllib/request.py:1349, in AbstractHTTPHandler.do_open(self, http_class, req, **http_conn_args)
   1346         h.request(req.get_method(), req.selector, req.data, headers,
   1347                   encode_chunked=req.has_header('Transfer-encoding'))
   1348     except OSError as err: # timeout error
-> 1349         raise URLError(err)
   1350     r = h.getresponse()
   1351 except:

URLError: <urlopen error Remote end closed connection without response>
```

### Versions

```
Collecting environment information...
PyTorch version: 1.11.0
Is debug build: False
CUDA used to build PyTorch: 11.3
ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04.3 LTS (x86_64)
GCC version: (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
Clang version: Could not collect
CMake version: version 3.16.3
Libc version: glibc-2.31

Python version: 3.9.7 (default, Sep 16 2021, 13:09:58)  [GCC 7.5.0] (64-bit runtime)
Python platform: Linux-4.15.0-101-generic-x86_64-with-glibc2.31
Is CUDA available: True
CUDA runtime version: Could not collect
GPU models and configuration: 
GPU 0: NVIDIA TITAN RTX
GPU 1: NVIDIA TITAN RTX

Nvidia driver version: 510.39.01
cuDNN version: Could not collect
...
[conda] numpy-base                1.21.2           py39h79a1101_0  
[conda] pytorch                   1.11.0          py3.9_cuda11.3_cudnn8.2.0_0    pytorch
[conda] pytorch-mutex             1.0                        cuda    pytorch
[conda] torchvision               0.12.0               py39_cu113    pytorch
```

cc @fmassa @vfdev-5 @pmeier @nairbv @NicolasHug @vmoens @jdsgomes