C# 转换中的 HttpURLConnection REST API
Posted
技术标签:
【中文标题】C# 转换中的 HttpURLConnection REST API【英文标题】:HttpURLConnection REST API in C# conversion 【发布时间】:2019-09-21 11:48:02 【问题描述】:我刚得到一些 Java 代码,我需要将其转换为 C# 并将文件上传到 Documentum 服务器。我转换成C#,需要一些查询。
边界是什么(在 Java 中使用一些常量唯一值)在 C# 中声明什么,我希望添加标题“a_content_type”和“object_name”。 C#中的SetTimeOut、ReadTimeOut、UseCatches、DoInput和DoOutput是什么?
Java 代码:
private HttpURLConnection getHttpURLConnection( String fileName, String fileExt, URL urlOne ) throws IOException
HttpURLConnection connection;
connection = ( HttpURLConnection ) urlOne.openConnection();
connection.setRequestMethod( "POST" );
connection.setRequestProperty( CONTENT_TYPE, "multipart/form-
data;boundary="+ BOUNDARY );
connection.setRequestProperty( ACCEPT, "application/vnd.emc.documentum+json" );
connection.setRequestProperty( AUTHORIZATION,BASIC+
Base64.encodeBase64String( ( username + ":" + password ) .getBytes() ) );
connection.setUseCaches( false );
connection.setRequestProperty( "Object_Name", "test_pdffile");
connection.setRequestProperty( A_CONTENT_TYPE, "pdf" );
connection.setDoInput( true );
connection.setDoOutput( true );
connection.setConnectTimeout(TIMEOUT * THOUSAND);
connection.setReadTimeout(TIMEOUT * THOUSAND);
return connection;
C#代码:
try
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "multipart/form-data;boundary="+ BOUNDARY;
request.PreAuthenticate = true;
String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));
request.Headers.Add("Authorization", "Basic " + encoded);
request.Accept = "application/vnd.emc.documentum+json";
request.Headers["Object_Name"] = "test_pdffile";
request.Headers["a_content_type"] = "pdf";
【问题讨论】:
您到底希望我们如何知道您的应用程序中定义的任意常量的值?你为什么不,我不知道,使用这些常量的值? 【参考方案1】:这是我使用的代码。它正在工作。
D2Document newDocument = new D2Document();
newDocument.SetPropertyValue("object_name", fileName);
newDocument.SetPropertyValue("a_content_type", contenType);
String documentURL = ConfigurationManager.AppSettings["DOCUMENTUM_URL"] + "objects/"+ documentId + "/content-media?format=" + contenType + "&modifier=&page=0";
JSON_GENERIC_MEDIA_TYPE = new MediaTypeWithQualityHeaderValue("application/json");
JSON_VND_MEDIA_TYPE = new MediaTypeWithQualityHeaderValue("application/vnd.emc.documentum+json");
try
using (var multiPartStream = new MultipartFormDataContent())
MemoryStream stream = new MemoryStream();
JsonSerializer.WriteObject(stream, newDocument);
ByteArrayContent firstPart = new ByteArrayContent(stream.ToArray());
firstPart.Headers.ContentType = JSON_VND_MEDIA_TYPE;
multiPartStream.Add(firstPart);
stream.Dispose();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, documentURL);
request.Content = multiPartStream;
String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));
request.Headers.Add("Authorization", "Basic " + encoded);
using (HttpResponseMessage response = _httpClient.GetAsync(documentURL).Result)
if (response != null)
var responsestream = response.Content;
【讨论】:
【参考方案2】:对于边界 - 看here
“a_content_type”和“object_name”确实是标题 SetTimeOut 是:request.Timeout ReadTimeOut 是 request.ReadWriteTimeout
UseCatches、DoInput 和 DoOutput 用于 Java 特定用途,据我所知,它们在 C# 中没有任何等价物。
【讨论】:
非常感谢!欣赏。知道我们如何在 C# 中使用 BOUNDARY 吗? NP,请参阅ec.haxx.se/http-multipart.html 以了解有关边界部分的更多信息,因为它不是 c# 特定的问题,而是一个。如果我的回答对您有帮助,请将其标记为已接受的答案。以上是关于C# 转换中的 HttpURLConnection REST API的主要内容,如果未能解决你的问题,请参考以下文章
Android中的HttpURLConnection有线记录
android中的HttpUrlConnection的使用之二
android中的HttpUrlConnection的使用之一
HttpURLConnection 返回的 InputStream 中的数据在你读取之前存储在哪里?