【发布时间】:2016-07-13 11:14:06
【问题描述】:
我正在尝试通过 RESTful Web 服务下载文件,然后将文件保存在计算机上。
我正在使用 PDF 文件来测试代码。我发现数据是 UTF-8 编码的,所以我尝试将其编码回默认值,因为我通过在本地读取 pdf 文件并再次将其写回发现它是这样工作的。
这是我的代码:
IConsumerRequest getDocumentRequest = class.consumerSession
.Request()
.ForMethod("GET")
.ForUri(new Uri(class.apiEndpoint + "/1/documents/" + id))
.SignWithToken(class.accessToken);
string test = System.IO.File.ReadAllText("C:\\test.pdf", Encoding.Default);
byte[] bytes = Encoding.UTF8.GetBytes(getDocumentRequest.ToString());
string data = Encoding.Default.GetString(bytes);
MessageBox.Show(test.Substring(0, 120) + "\n\n" + data.Substring(0, 120));
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.FileName = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
System.IO.File.WriteAllBytes(saveFileDialog.FileName, bytes);
}
比较字符串显示以下内容(第二行): Local String vs String from Webservice
我已经尝试了几种方法来转换字符串,没有任何区别。
【问题讨论】: