Skip to content

Commit 3e0db62

Browse files
committed
AddFormDataText简化
1 parent 7f451f3 commit 3e0db62

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed

WebApiClientCore/Implementations/HttpApiRequestMessageImpl.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ sealed class HttpApiRequestMessageImpl : HttpApiRequestMessage
2525
/// <summary>
2626
/// 请求头的默认UserAgent
2727
/// </summary>
28-
private readonly static ProductInfoHeaderValue defaultUserAgent = new(assemblyName.Name ?? "WebApiClientCore", assemblyName.Version?.ToString());
28+
private readonly static ProductInfoHeaderValue defaultUserAgent = new(assemblyName.Name ?? nameof(WebApiClientCore), assemblyName.Version?.ToString());
2929

3030
/// <summary>
3131
/// httpApi的请求消息
@@ -123,12 +123,7 @@ private static string GetRelativeUri(Uri uri)
123123

124124
var path = uri.OriginalString.AsSpan()[(uri.Scheme.Length + 3)..];
125125
var index = path.IndexOf('/');
126-
if (index < 0)
127-
{
128-
return "/";
129-
}
130-
131-
return path[index..].ToString();
126+
return index < 0 ? "/" : path[index..].ToString();
132127
}
133128

134129
/// <summary>
@@ -182,16 +177,16 @@ public override void AddFormDataText(IEnumerable<KeyValue> keyValues)
182177
{
183178
this.EnsureMediaTypeEqual(FormDataContent.MediaType);
184179

185-
if (!(this.Content is MultipartContent httpContent))
180+
if (this.Content is not MultipartContent httpContent)
186181
{
187182
httpContent = new FormDataContent();
183+
this.Content = httpContent;
188184
}
189185

190186
foreach (var keyValue in keyValues)
191187
{
192188
var textContent = new FormDataTextContent(keyValue);
193189
httpContent.Add(textContent);
194-
this.Content = httpContent;
195190
}
196191
}
197192

@@ -209,14 +204,14 @@ public override void AddFormDataFile(Stream stream, string name, string? fileNam
209204
{
210205
this.EnsureMediaTypeEqual(FormDataContent.MediaType);
211206

212-
if (!(this.Content is MultipartContent httpContent))
207+
if (this.Content is not MultipartContent httpContent)
213208
{
214209
httpContent = new FormDataContent();
210+
this.Content = httpContent;
215211
}
216212

217213
var fileContent = new FormDataFileContent(stream, name, fileName, contentType);
218214
httpContent.Add(fileContent);
219-
this.Content = httpContent;
220215
}
221216

222217
/// <summary>

0 commit comments

Comments
 (0)