系列目錄???? 【已更新最新開發(fā)文章,點(diǎn)擊查看詳細(xì)】
<https://www.cnblogs.com/SavionZhang/p/11422481.html>
獲取或設(shè)置請求的?Content-type?HTTP 標(biāo)頭的值。默認(rèn)值為null。
常見的請求內(nèi)容類型為以下幾種:
1 /// <summary> 2 /// HTTP 內(nèi)容類型(Content-Type) 3 /// </summary> 4 public
class HttpContentType 5 { 6 /// <summary> 7 /// 資源類型:普通文本 8 /// </summary>
9 public const string TEXT_PLAIN = "text/plain"; 10 11 /// <summary> 12 ///
資源類型:JSON字符串13 /// </summary> 14 public const string APPLICATION_JSON = "
application/json"; 15 16 /// <summary> 17 /// 資源類型:未知類型(數(shù)據(jù)流) 18 /// </summary>
19 public const string APPLICATION_OCTET_STREAM = "application/octet-stream"; 20
21 /// <summary> 22 /// 資源類型:表單數(shù)據(jù)(鍵值對) 23 /// </summary> 24 public const string
WWW_FORM_URLENCODED ="application/x-www-form-urlencoded"; 25 26 /// <summary> 27
/// 資源類型:表單數(shù)據(jù)(鍵值對)。編碼方式為 gb2312 28 /// </summary> 29 public const string
WWW_FORM_URLENCODED_GB2312 ="application/x-www-form-urlencoded;charset=gb2312";
30 31 /// <summary> 32 /// 資源類型:表單數(shù)據(jù)(鍵值對)。編碼方式為 utf-8 33 /// </summary> 34
public const string WWW_FORM_URLENCODED_UTF8 = "
application/x-www-form-urlencoded;charset=utf-8"; 35 36 /// <summary> 37 ///
資源類型:多分部數(shù)據(jù)38 /// </summary> 39 public const string MULTIPART_FORM_DATA = "
multipart/form-data"; 40 }
提交的時候可以說明編碼的方式,用來使對方服務(wù)器能夠正確的解析。
該ContentType的
<https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebrequest.contenttype?view=netframework-4.8>
屬性包含請求的媒體類型。分配給ContentType
<https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebrequest.contenttype?view=netframework-4.8>
屬性的值在請求發(fā)送Content-typeHTTP標(biāo)頭時替換任何現(xiàn)有內(nèi)容。
要清除Content-typeHTTP標(biāo)頭,請將ContentType
<https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebrequest.contenttype?view=netframework-4.8>
屬性設(shè)置為null。
此屬性的值存儲在WebHeaderCollection中。如果設(shè)置了WebHeaderCollection,則屬性值將丟失。
?參考示例代碼:
1 private HttpResult Request(string url, string data, string method, string
contentType) 2 { 3 HttpResult httpResult = new HttpResult(); 4 HttpWebRequest
httpWebRequest =null; 5 6 try 7 { 8 httpWebRequest = WebRequest.Create(url)
as HttpWebRequest; 9 httpWebRequest.Method = method; 10 httpWebRequest.Headers
= HeaderCollection; 11 httpWebRequest.CookieContainer = CookieContainer;
12 /*此屬性的值存儲在WebHeaderCollection中。如果設(shè)置了WebHeaderCollection,則屬性值將丟失。 13
*所以放置在Headers 屬性之后設(shè)置14 */ 15 httpWebRequest.ContentType = contentType; 16
httpWebRequest.UserAgent = _userAgent; 17 httpWebRequest.AllowAutoRedirect =
_allowAutoRedirect;18 httpWebRequest.ServicePoint.Expect100Continue = false; 19
20 if (data != null) 21 { 22 httpWebRequest.AllowWriteStreamBuffering = true;
23 using (Stream requestStream = httpWebRequest.GetRequestStream()) 24 { 25
requestStream.Write(EncodingType.GetBytes(data),0, data.Length); 26
requestStream.Flush();27 } 28 } 29 30 HttpWebResponse httpWebResponse =
httpWebRequest.GetResponse()as HttpWebResponse; 31 if (httpWebResponse != null)
32 { 33 GetResponse(ref httpResult, httpWebResponse); 34
httpWebResponse.Close();35 } 36 } 37 catch (WebException webException) 38 {
39 GetWebExceptionResponse(ref httpResult, webException); 40 } 41 catch
(Exception ex)42 { 43 GetExceptionResponse(ref httpResult, ex, method,
contentType);44 } 45 finally 46 { 47 if (httpWebRequest != null) 48 { 49
httpWebRequest.Abort();50 } 51 } 52 53 return httpResult; 54 }
?
系列目錄???? 【已更新最新開發(fā)文章,點(diǎn)擊查看詳細(xì)】
<https://www.cnblogs.com/SavionZhang/p/11422481.html>
熱門工具 換一換