前言
报错原因:From需要是邮件地址的格式。
协议头 | 说明 | 示例 | 状态 |
---|---|---|---|
From | 发起此请求的用户的邮件地址 | From: user@itbilu.com |
固定 |
参考:
https://blog.csdn.net/m0_37730732/article/details/82263609
https://itbilu.com/other/relate/EJ3fKUwUx.html#http-request-headers
http://www.iana.org/assignments/message-headers/message-headers.xhtml
代码
HttpRequestMessage httpRequestMessage = new HttpRequestMessage(); httpRequestMessage.Headers.Add("from", "mywork"); httpRequestMessage.Headers.Add("timestamp", timestamp); httpRequestMessage.Headers.Add("signature", signature); httpRequestMessage.Headers.Add("client-name", client_name); httpRequestMessage.Method = HttpMethod.Get; httpRequestMessage.RequestUri = new Uri(Domain + requestUrl); HttpResponseMessage response = await _client.SendAsync(httpRequestMessage); string r = string.Empty; if (response.IsSuccessStatusCode) r = await response.Content.ReadAsStringAsync(); return r;
错误信息
The format of value 'mywork' is invalid.
at System.Net.Http.Headers.HttpHeaderParser.ParseValue(String value, Object storeValue, Int32& index)
at System.Net.Http.Headers.HttpHeaders.ParseAndAddValue(HeaderDescriptor descriptor, HeaderStoreItemInfo info, String value)
at System.Net.Http.Headers.HttpHeaders.Add(String name, String value)
解决办法
目前没找到合适的处理方式,暂时使用HttpWebRequest请求
public static string HttpGet(string url, string Accept, string ContentType, Dictionary<string, string> headers) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; if (Accept.NotEmpty()) request.Accept = Accept; if (ContentType.NotEmpty()) request.ContentType = ContentType; if (headers != null) foreach (KeyValuePair<string, string> item in headers) { request.Headers.Add(item.Key, item.Value); } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { return reader.ReadToEnd(); } }