HttpWebRequest.AddRange Method (Int32)

41 篇文章 0 订阅

public void AddRange(
	int range
)

The HttpWebRequest.AddRange method adds a byte range header to the request.

If range is positive, the range parameter specifies the starting point of the range. The server should start sending data from the range parameter specified to the end of the data in the HTTP entity.

If range is negative, the range parameter specifies the ending point of the range. The server should start sending data from the start of the data in the HTTP entity to the range parameter specified.

Since all HTTP entities are represented in HTTP messages as sequences of bytes, the concept of a byte range is meaningful for any HTTP entity. However, not all clients and servers need to support byte-range operations.

The Range header on a request allows a client to request that it only wants to receive some part of the specified range of bytes in an HTTP entity. Servers are not required to support Range header requests.

An example of a Range header in an HTTP protocol request that requests the server send the first 100 bytes (from the start to byte position 99) would be the following:

Range: bytes=0-99\r\n\r\n

For this example, the range parameter would be -99.

A HTTP server indicates support for Range headers with the Accept-Ranges header. An example of the Accept-Ranges header from a server that supports byte-ranges would be as follows:

Accept-Ranges: bytes\r\n\r\n

If an Accept-Ranges header is not received in the header of the response from the server, then the server does not support Range headers. An example of the Accept-Ranges header from a server that does not support ranges, but recognizes the Accept-Ranges header, would be as follows:

Accept-Ranges: none\r\n\r\n

When receiving the response from a range request, only the HTTP headers associated with the entire request are parsed and made available via properties on the HttpWebResponse class. Headers associated with each range are returned in the response.

The following code example adds a range header to the request.

			// Create a New 'HttpWebRequest' object .
			HttpWebRequest myHttpWebRequest1=(HttpWebRequest)WebRequest.Create("http://www.contoso.com");
			myHttpWebRequest1.AddRange(1000);	
			Console.WriteLine("Call AddRange(1000)");
			Console.Write("Resulting Headers: ");
			Console.WriteLine(myHttpWebRequest1.Headers.ToString());
			
			// Create a New 'HttpWebRequest' object .
			HttpWebRequest myHttpWebRequest2=(HttpWebRequest)WebRequest.Create("http://www.contoso.com");
			myHttpWebRequest2.AddRange(-1000);	
			Console.WriteLine("Call AddRange(-1000)");
			Console.Write("Resulting Headers: ");
			Console.WriteLine(myHttpWebRequest2.Headers.ToString());


public static T Post<T>(string url, string data, string staffId, string contentType) { try { byte[] bytes = Encoding.UTF8.GetBytes(data); HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url); string timeStamp = ApiHelper.GetTimeStamp(); string random = ApiHelper.GetRandom(); httpWebRequest.Headers.Add("staffid", staffId.ToString()); httpWebRequest.Headers.Add("timestamp", timeStamp); httpWebRequest.Headers.Add("nonce", random); //ResultMsg signToken = ApiHelper.GetToken(url, staffId); // httpWebRequest.Headers.Add("signature", ApiHelper.GetSignature(timeStamp, random, staffId, data, signToken)); httpWebRequest.Headers.Add("jwtcookie", GetJwt(url, data));//cwj 直接通过jwt验证身份 httpWebRequest.Method = "POST"; httpWebRequest.ContentLength = (long)bytes.Length; httpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"; httpWebRequest.ContentType = contentType; Stream requestStream = httpWebRequest.GetRequestStream(); requestStream.Write(bytes, 0, bytes.Length); httpWebRequest.Timeout = 300000; httpWebRequest.Headers.Set("Pragma", "no-cache"); HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); Stream responseStream = httpWebResponse.GetResponseStream(); StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8); string text = streamReader.ReadToEnd(); requestStream.Close(); streamReader.Close(); responseStream.Close(); httpWebRequest.Abort(); httpWebResponse.Close(); return JsonConvert.DeserializeObject<T>(text); } catch (Exception) { throw; } }
06-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值