使用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)
在初始化的时候就定义BaseAddress呗
哎呀妈,被dudu老大采纳了,受宠若惊!
private static readonly HttpClient _httpClient = new HttpClient() { BaseAddress = new Uri("http://xxx") };
@dudu: 是的,没错!
dudu 老大也有问题咨询呀,帮你顶了!!!
我还以为java类库~
不清楚你上下文,但堆栈貌似很明白。
不过还是推荐这种低级类库别用了,直接用RestSharp,我写外挂有时都用这玩意儿,认证、序列化等等封装都比较可以。