猿问

C#中HttpClient问题"Properties can only be modified before sending the first request"

使用HttpClient的C#代码如下:

public class TestClass
{
   private static readonly HttpClient _httpClient = new HttpClient();
   public async Task TestMethod ()
   { 
       //...
       _httpClient.BaseAddress = new Uri("http://xxx");
       //...
    }
}

运行时报错

System.InvalidOperationException: This instance has already started one or more requests. Properties can only be modified before sending the first request.
   at System.Net.Http.HttpClient.CheckDisposedOrStarted()
   at System.Net.Http.HttpClient.set_BaseAddress(Uri value)
弑天下
浏览 1561回答 4
4回答

慕雪6442864

在初始化的时候就定义BaseAddress呗

慕的地6264312

我还以为java类库~ 不清楚你上下文,但堆栈貌似很明白。 不过还是推荐这种低级类库别用了,直接用RestSharp,我写外挂有时都用这玩意儿,认证、序列化等等封装都比较可以。

小怪兽爱吃肉

HttpClient - This instance has already started
随时随地看视频慕课网APP
我要回答