Поделиться через


WebRequest.ContentType Свойство

Определение

При переопределении в классе-потомке получает или задает тип контента отправляемых данных запроса.

public:
 abstract property System::String ^ ContentType { System::String ^ get(); void set(System::String ^ value); };
public:
 virtual property System::String ^ ContentType { System::String ^ get(); void set(System::String ^ value); };
public abstract string ContentType { get; set; }
public virtual string? ContentType { get; set; }
public virtual string ContentType { get; set; }
member this.ContentType : string with get, set
Public MustOverride Property ContentType As String
Public Overridable Property ContentType As String

Значение свойства

Тип контента данных запроса.

Исключения

Любая попытка получить или задать свойство, если свойство не переопределяется в классе-потомке.

Примеры

В следующем примере свойство присваивается ContentType соответствующему типу носителя.


// Set the 'ContentType' property of the WebRequest.
myWebRequest.ContentType="application/x-www-form-urlencoded";

// Set the 'ContentLength' property of the WebRequest.
myWebRequest.ContentLength=byteArray.Length;
Stream newStream=myWebRequest.GetRequestStream();
newStream.Write(byteArray,0,byteArray.Length);

// Close the Stream object.
newStream.Close();

// Assign the response object of 'WebRequest' to a 'WebResponse' variable.
WebResponse myWebResponse=myWebRequest.GetResponse();


' Set the 'ContentType' property of the WebRequest.
myWebRequest.ContentType = "application/x-www-form-urlencoded"

' Set the 'ContentLength' property of the WebRequest.
myWebRequest.ContentLength = byteArray.Length
Dim newStream As Stream = myWebRequest.GetRequestStream()
newStream.Write(byteArray, 0, byteArray.Length)

' Close the Stream object.
newStream.Close()

' Assign the response object of 'WebRequest' to a 'WebResponse' variable.
Dim myWebResponse As WebResponse = myWebRequest.GetResponse()

Комментарии

Осторожность

WebRequest, HttpWebRequest, ServicePointи устарели, и WebClient вы не должны использовать их для новой разработки. Вместо этого используйте HttpClient.

Свойство ContentType содержит тип носителя запроса. Обычно это кодировка MIME содержимого.

Заметка

Класс WebRequest является классом abstract . Фактическое поведение WebRequest экземпляров во время выполнения определяется классом-потомком, возвращаемым методом WebRequest.Create . Дополнительные сведения о значениях и исключениях по умолчанию см. в документации для классов-потомков, таких как HttpWebRequest и FileWebRequest.

Применяется к

См. также раздел