简体   繁体   English

帮助将java代码转换为C#

[英]help to convert java code to C#

i was trying to get the C# version of the following java code snippet, 我试图获得以下java代码片段的C#版本,

HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestProperty("Range", "bytes=1024-");

this is what i have so far 这就是我到目前为止所拥有的

 WebRequest request = WebRequest.Create(someUri);
 request.Headers.Add("Range", "bytes=1024-");

but it is not working,what is the right way for me go? 但它不起作用,对我来说什么是正确的方法?

Presumably your URI is HTTP since Java's HttpURLConnection is designed for a HTTP connection. 据推测,您的URI是HTTP,因为Java的HttpURLConnection是为HTTP连接而设计的。 WebRequest is abstract and can handle multiple protocols. WebRequest是抽象的,可以处理多种协议。 However, by specifiying a HttpWebRequest type, you can access HTTP-specific methods. 但是,通过指定HttpWebRequest类型,您可以访问特定于HTTP的方法。 The Range header is protected and you should use AddRange to set the property instead of directly adding it to the Header collection. Range标头受保护 ,您应该使用AddRange设置属性,而不是直接将其添加到Header集合。

HttpWebRequest request = (HttpWebRequest) WebRequest.Create(someUri);
request.AddRange("bytes",1024);

You are setting two different things. 你正在设置两个不同的东西。

A request property is a value passed to the page. 请求属性是传递给页面的值。

A header property is a header in the HTTP request. 标头属性是HTTP请求中的标头。 Something like setting the HTTP REFERER (sic). 像设置HTTP REFERER(原文如此)的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM