当我在C#中使用restAPI时,我需要将以下代码从Java转换为C#。
在java中:
HttpGet statusGet = new HttpGet(fileUrl);
statusGet.setHeader("X-API-TOKEN", API_TOKEN);
HttpResponse response = httpClient.execute(statusGet);
// Extract exported file
ZipInputStream zs = new ZipInputStream(response.getEntity().getContent());
在C#中,这就是我所拥有的:
var client1 = new RestClient(fileUrl);
var request1 = new RestRequest(Method.GET);
request1.AddHeader("X-API-TOKEN", "API_TOKEN");
request1.AddHeader("content-type", "application/json");
request1.AddParameter("application/json", "{\n\t\"format\" : \"csv\",\n\t\"surveyId\" : \"_surveyId\"\n}", ParameterType.RequestBody);
IRestResponse responsedata = client1.Execute(request1);
var download=client1.DownloadData(request1);
MemoryStream stream = new MemoryStream(download);
ZipInputStream zs = new ZipInputStream(stream);
using (ZipFile zip1 = ZipFile.Read(zs))
我不知道如何实现response.getEntity()。getContent()。我相信它正在获取Stream(包含一个zip文件?) 更新:所以我从client1.DownloadData(request1)获取字节数组,看起来将它转换为流是不对的(具有readtimeout异常)。它将无法从zipfile.read读取 非常感谢你的帮助
答案 0 :(得分:0)
您是否收到任何具体错误?您似乎正在使用RestSharp实现此功能。您是否按照他们的示例阅读了他们的文档?
我没有亲自使用过这个第三方解决方案,但是在他们的首页上,他们有以下示例,它完全符合您的要求:
var client = new RestClient("http://example.com"); // client.Authenticator = new HttpBasicAuthenticator(username, password); var request = new RestRequest("resource/{id}", Method.POST); request.AddParameter("name", "value"); // adds to POST or URL querystring based on Method request.AddUrlSegment("id", "123"); // replaces matching token in request.Resource // easily add HTTP Headers request.AddHeader("header", "value"); // add files to upload (works with compatible verbs) request.AddFile(path); // execute the request IRestResponse response = client.Execute(request); var content = response.Content; // raw content as string // or automatically deserialize result // return content type is sniffed but can be explicitly set via RestClient.AddHandler(); RestResponse<Person> response2 = client.Execute<Person>(request); var name = response2.Data.Name; // easy async support client.ExecuteAsync(request, response => { Console.WriteLine(response.Content); }); // async with deserialization var asyncHandle = client.ExecuteAsync<Person>(request, response => { Console.WriteLine(response.Data.Name); }); // abort the request on demand asyncHandle.Abort();
您似乎希望访问IRestResponse.Content
属性,或使用RestClient.Execute<T>(RestRequest request)
函数反序列化。