HttpWebRequest.SendChunked Property

Gets or sets a value that indicates whether to send data in segments to the Internet resource.

Namespace:  System.Net
Assembly:  System (in System.dll)

public bool SendChunked { get; set; }

Property Value

Type: System.Boolean
true to send data to the Internet resource in segments; otherwise, false. The default value is false.

ExceptionCondition
InvalidOperationException

The request has been started by calling the GetRequestStream, BeginGetRequestStream, GetResponse, or BeginGetResponse method.

When SendChunked is true, the request sends data to the Internet resource in segments. The Internet resource must support receiving chunked data.

Changing the SendChunked property after the request has been started by calling the GetRequestStream, BeginGetRequestStream, GetResponse, or BeginGetResponse method throws an InvalidOperationException.

The following code example sets the SendChunked property to true so that data can be sent in segments to the Internet resource.

// A new 'HttpWebRequest' object is created.
HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create(myUri);
myHttpWebRequest.SendChunked=true;
// 'TransferEncoding' property is set to 'gzip'.
myHttpWebRequest.TransferEncoding="gzip";
Console.WriteLine("\nPlease Enter the data to be posted to the (http://<machine name>/CodeSnippetTest.asp) uri:");
string inputData =Console.ReadLine();
string postData="testdata="+inputData;
// 'Method' property of 'HttpWebRequest' class is set to POST.
myHttpWebRequest.Method="POST";
ASCIIEncoding encodedData=new ASCIIEncoding();
byte[]  byteArray=encodedData.GetBytes(postData);
// 'ContentType' property of the 'HttpWebRequest' class is set to "application/x-www-form-urlencoded".
myHttpWebRequest.ContentType="application/x-www-form-urlencoded";
// 'ContentLength' property is set to Length of the data to be posted.
myHttpWebRequest.ContentLength=byteArray.Length;
Stream newStream=myHttpWebRequest.GetRequestStream();
newStream.Write(byteArray,0,byteArray.Length);
newStream.Close();
Console.WriteLine("\nData has been posted to the Uri\n\nPlease wait for the response..........");
// The response object of 'HttpWebRequest' is assigned to a 'HttpWebResponse' variable.
HttpWebResponse myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();
// Displaying the contents of the page to the console
Stream streamResponse=myHttpWebResponse.GetResponseStream();
StreamReader streamRead = new StreamReader( streamResponse );
Char[] readBuff = new Char[256];
int count = streamRead.Read( readBuff, 0, 256 );
Console.WriteLine("\nThe contents of the HTML page are :  ");
while (count > 0) 
{
   String outputData = new String(readBuff, 0, count);
   Console.WriteLine(outputData);
   count = streamRead.Read(readBuff, 0, 256);
}
// Release the response object resources.
streamRead.Close();
streamResponse.Close();
myHttpWebResponse.Close(); 
// A new 'HttpWebRequest' object is created.
HttpWebRequest* myHttpWebRequest =
   dynamic_cast<HttpWebRequest*>(WebRequest::Create(myUri));
myHttpWebRequest->SendChunked = true;
// 'TransferEncoding' property is set to 'gzip'.
myHttpWebRequest->TransferEncoding = S"gzip";
Console::WriteLine(S"\nPlease Enter the data to be posted to the (http://<machine name>/CodeSnippetTest::asp) uri:");
String* inputData = Console::ReadLine();
String* postData=String::Concat(S"testdata= ", inputData);
// 'Method' property of 'HttpWebRequest' class is set to POST.
myHttpWebRequest->Method = S"POST";
ASCIIEncoding* encodedData = new ASCIIEncoding();
Byte byteArray[] = encodedData->GetBytes(postData);
// 'ContentType' property of the 'HttpWebRequest' class is set to S"application/x-www-form-urlencoded".
myHttpWebRequest->ContentType = S"application/x-www-form-urlencoded";
// 'ContentLength' property is set to Length of the data to be posted.
myHttpWebRequest->ContentLength = byteArray->Length;
Stream* newStream = myHttpWebRequest->GetRequestStream();
newStream->Write(byteArray, 0, byteArray->Length);
newStream->Close();
Console::WriteLine(S"\nData has been posted to the Uri\n\nPlease wait for the response..........");
// The response object of 'HttpWebRequest' is assigned to a 'HttpWebResponse' variable.
HttpWebResponse* myHttpWebResponse =
   dynamic_cast<HttpWebResponse*>(myHttpWebRequest->GetResponse());
// Displaying the contents of the page to the console
Stream* streamResponse = myHttpWebResponse->GetResponseStream();
StreamReader* streamRead = new StreamReader(streamResponse);
Char readBuff[] = new Char[256];
int count = streamRead->Read(readBuff, 0, 256);
Console::WriteLine(S"\nThe contents of the HTML page are :  ");
while (count > 0) {
   String* outputData = new String(readBuff, 0, count);
   Console::WriteLine(outputData);
   count = streamRead->Read(readBuff, 0, 256);
}
// Release the response object resources.
streamRead->Close();
streamResponse->Close();
myHttpWebResponse->Close();

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft