如何从 Java 转换为 Xamarin C#

How to convert from Java to Xamarin C#

有人可以帮助我将以下内容从 Java 转换为 C# (Xamarin) 吗?

我尝试了几种不同的方法,但无法正常工作。

密码是:

HttpPost post = new HttpPost(url);

// Break out all extra HTTP header lines and add it to the HttpPost object
for (String line : contentType.replace("\r", "\n").split("\n")) {
    if (line.length() > 0 && line.contains(":")) {
        String[] parts = line.split(":", 2);
        if (parts.length == 2) {
            post.addHeader(parts[0].trim(), parts[1].trim());
        }
    }
}

// Create a byte array entity for the POST data, the content
// type here is only used for the postEntity object
ByteArrayEntity postEntity = new ByteArrayEntity(challenge);
postEntity.setContentType("application/octet-stream");
post.setEntity(postEntity);

// Create a HttpClient and execute the HttpPost filled out above
HttpClient client = new DefaultHttpClient();
HttpResponse httpResponse = client.execute(post);

// Get the response entity out of the response
HttpEntity entity = httpResponse.getEntity();

如果你被困

post.SetEntity(postEntity);

然后它转换为:

ByteArrayEntity postEntity = new ByteArrayEntity(challenge);
postEntity.SetContentType("application/octet-stream");
post.Entity = postEntity;

从 C# 转换为 Java 时,您通常必须将 属性 名称更改为以大写字母开头,然后如果您卡在某些对象上,我会查看 Xamarin API 文档,HttpPost class 链接 here.