# How to increase the timeout of a Invoke-WebRequest/HttpWebRequest API call either globally or in web.config?

**URL:** <https://forums.powershell.org/t/how-to-increase-the-timeout-of-a-invoke-webrequest-httpwebrequest-api-call-either-globally-or-in-web-config/24619>\
**Category:** PowerShell Help\
**Created:** [August 22, 2024, 6:58pm UTC](https://forums.powershell.org/t/how-to-increase-the-timeout-of-a-invoke-webrequest-httpwebrequest-api-call-either-globally-or-in-web-config/24619 "2024-08-22T18:58:57Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![helpdeskaleer](https://avatars.discourse-cdn.com/v4/letter/h/9de053/32.png) [@helpdeskaleer](https://forums.powershell.org/u/helpdeskaleer)\
**Post date:** [August 22, 2024, 6:58pm UTC](https://forums.powershell.org/t/how-to-increase-the-timeout-of-a-invoke-webrequest-httpwebrequest-api-call-either-globally-or-in-web-config/24619/1 "2024-08-22T18:58:57Z")

</div>

If your program uses the now legacy API `System.Net.HttpWebRequest` and that `HttpWebRequest` throws a `System.Net.WebException` that reads `The operation has timed out`.

Is it possible to change the `timeout` property in the `System.Net.WebException` via a `web.config` file by adding the correct `add` element `name` and `value` attribute?

Or possibly change it globally using the registry or a GPO?

I have already been advised to fix this using `netsh` http retries and some other settings and truth be told the issue could be on the opposite end of the connection where it also could be timing out; but it is unclear how to find that out.

The error looks like this:

```auto
System.Net.WebException: The operation has timed out.
  at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)
  at System.Net.HttpWebRequest.GetRequestStream()
  at System.WebServices.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters)
  ....

```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [August 22, 2024, 8:10pm UTC](https://forums.powershell.org/t/how-to-increase-the-timeout-of-a-invoke-webrequest-httpwebrequest-api-call-either-globally-or-in-web-config/24619/2 "2024-08-22T20:10:09Z")

</div>

Could you clarify on how this relates to Powershell? I do see you mention Invoke-WebRequest but that command simply sends an HTTP request to a service, so don’t think it’s at the core of what you are trying to accomplish here.

---

<div class="post-metadata">

**Author:** ![helpdeskaleer](https://avatars.discourse-cdn.com/v4/letter/h/9de053/32.png) [@helpdeskaleer](https://forums.powershell.org/u/helpdeskaleer)\
**Post date:** [August 22, 2024, 8:19pm UTC](https://forums.powershell.org/t/how-to-increase-the-timeout-of-a-invoke-webrequest-httpwebrequest-api-call-either-globally-or-in-web-config/24619/3 "2024-08-22T20:19:54Z")

</div>

Sure; I realize it’s a bit of a stretch but;

I imagine the `Invoke-WebRequest` cmdlet and the `.NET` `System.Net.HttpWebRequest` library to use the same library to make calls to webpages; webservices and such.

So I figured that within the context of a web application since they both use the same library; maybe both could be effected by some global windows setting in the registry or through `web.config` in a `.net` webapp or using an argument about timeouts such as `[-TimeoutSec <System.Int32>]` that would change the same argument in the library function call of `System.Net.HttpWebRequest`.

---

<div class="post-metadata">

**Author:** ![neemobeer](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/neemobeer/32/4087_2.png) [@neemobeer](https://forums.powershell.org/u/neemobeer)\
**Post date:** [August 22, 2024, 8:22pm UTC](https://forums.powershell.org/t/how-to-increase-the-timeout-of-a-invoke-webrequest-httpwebrequest-api-call-either-globally-or-in-web-config/24619/4 "2024-08-22T20:22:27Z")

</div>

You can simply use the -Timeout switch of Invoke-WebRequest

---

<div class="post-metadata">

**Author:** ![helpdeskaleer](https://avatars.discourse-cdn.com/v4/letter/h/9de053/32.png) [@helpdeskaleer](https://forums.powershell.org/u/helpdeskaleer)\
**Post date:** [August 22, 2024, 8:23pm UTC](https://forums.powershell.org/t/how-to-increase-the-timeout-of-a-invoke-webrequest-httpwebrequest-api-call-either-globally-or-in-web-config/24619/5 "2024-08-22T20:23:22Z")

</div>

Is there a global setting that affects this?

---

<div class="post-metadata">

**Author:** ![grey0ut](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/grey0ut/32/5310_2.png) [@grey0ut](https://forums.powershell.org/u/grey0ut)\
**Post date:** [August 22, 2024, 8:28pm UTC](https://forums.powershell.org/t/how-to-increase-the-timeout-of-a-invoke-webrequest-httpwebrequest-api-call-either-globally-or-in-web-config/24619/6 "2024-08-22T20:28:59Z")

</div>

i don’t think there’s a global setting for that, but you could leverage the built-in Default Parameter Values to make this **your** permanent setting:

> **[about Preference Variables - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_preference_variables?view=powershell-7.4#psdefaultparametervalues)**
>
> Variables that customize the behavior of PowerShell.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [August 22, 2024, 8:31pm UTC](https://forums.powershell.org/t/how-to-increase-the-timeout-of-a-invoke-webrequest-httpwebrequest-api-call-either-globally-or-in-web-config/24619/7 "2024-08-22T20:31:22Z")

</div>

I think it’s `TimeOutSec’ which in PS 7 i guess they changed how that works. I think OP hints at that potentially in response.

in any case in PS5

> Specifies how long the request can be pending before it times out. Enter a value in seconds. The default value, 0, specifies an indefinite time-out.  
> A Domain Name System (DNS) query can take up to 15 seconds to return or time out. If your request contains a host name that requires resolution, and you set TimeoutSec to a value greater than zero, but less than 15 seconds, it can take 15 seconds or more before a WebException is thrown, and your request times out.

My concern still is, that impacts that command only and I’m not convinced that’s really what OP is asking as they continually use the word ‘global’. They could use default parameter values in PS but again, this impacts just that command. I guess i’m still confused on the actual problem OP is attempting to solve. It might help to back up a couple steps and explain the situation instead of trying to ask how to implement a solution would be helpful to provide context. It seems like OP is running an app and wanting to control the timeout from the app or something. I might need more coffee too as i just woke up from a nap haha.

---

<div class="post-metadata">

**Author:** ![helpdeskaleer](https://avatars.discourse-cdn.com/v4/letter/h/9de053/32.png) [@helpdeskaleer](https://forums.powershell.org/u/helpdeskaleer)\
**Post date:** [August 22, 2024, 11:08pm UTC](https://forums.powershell.org/t/how-to-increase-the-timeout-of-a-invoke-webrequest-httpwebrequest-api-call-either-globally-or-in-web-config/24619/8 "2024-08-22T23:08:14Z")

</div>

> [@dotnVo](#):
>
> I might need more coffee too as i just woke up from a nap haha.

dotnVo; thank you for taking my question seriously.

Yes, I am running a web-services based `.NET` application; it like most .NET web-based applications has an XML configuration file in which you can specify `<add key="some-keyname" value="some-valuename" />`.

After the application reaches out to the enterprise server with a webservices request; eventually it times out; but the error message does not make it clear if we are talking about a timeout that comes from the enterprise server end or if the timeout is coming from the client end on the machines that I work on and can control.

It simply says in a dialog box that pops up with a title that reads `Error`

```auto
System.Net.WebException: The operation has timed out

```

(again, it doesn’t specify if the time out came from my end or the enterprise end)…and it goes on…with the rest of the stack trace to tell us where the exception came from:

```auto
at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)
at System.Net.HttpWebRequest.GetRequestStream()
at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters)
at EGatewayClientCE.EGatewayService.BDSWse.ProcessDimeRequest()
...etc

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [September 21, 2024, 11:08pm UTC](https://forums.powershell.org/t/how-to-increase-the-timeout-of-a-invoke-webrequest-httpwebrequest-api-call-either-globally-or-in-web-config/24619/9 "2024-09-21T23:08:20Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
