本文整理汇总了C#中ResponseStream.Read方法的典型用法代码示例。如果您正苦于以下问题:C# ResponseStream.Read方法的具体用法?C# ResponseStream.Read怎么用?C# ResponseStream.Read使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ResponseStream
的用法示例。
在下文中一共展示了ResponseStream.Read方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SaveFile
public void SaveFile(string fileId, string downloadUrl)
{
Global.Logger.Debug("GoogleDriveApp: save file stream " + fileId + " from - " + downloadUrl);
fileId = ThirdPartySelector.GetFileId(fileId);
var token = Token.GetToken(AppAttr);
var driveFile = GetDriveFile(fileId, token);
if (driveFile == null)
{
Global.Logger.Error("GoogleDriveApp: file is null");
throw new Exception("File not found");
}
var jsonFile = JObject.Parse(driveFile);
var curExt = GetCorrectExt(jsonFile);
var newExt = FileUtility.GetFileExtension(downloadUrl);
if (curExt != newExt)
{
try
{
Global.Logger.Debug("GoogleDriveApp: GetConvertedUri from " + newExt + " to " + curExt + " - " + downloadUrl);
var key = DocumentServiceConnector.GenerateRevisionId(downloadUrl);
DocumentServiceConnector.GetConvertedUri(downloadUrl, newExt, curExt, key, false, out downloadUrl);
}
catch (Exception e)
{
Global.Logger.Error("GoogleDriveApp: Error convert", e);
}
}
var downloadRequest = WebRequest.Create(downloadUrl);
using (var downloadResponse = downloadRequest.GetResponse())
using (var downloadStream = new ResponseStream(downloadResponse))
{
var request = (HttpWebRequest)WebRequest.Create(GoogleUrlUpload + "/{fileId}?uploadType=media".Replace("{fileId}", fileId));
request.Method = "PUT";
request.Headers.Add("Authorization", "Bearer " + token);
request.ContentType = downloadResponse.ContentType;
request.ContentLength = downloadResponse.ContentLength;
const int bufferSize = 2048;
var buffer = new byte[bufferSize];
int readed;
while ((readed = downloadStream.Read(buffer, 0, bufferSize)) > 0)
{
request.GetRequestStream().Write(buffer, 0, readed);
}
try
{
using (var response = request.GetResponse())
using (var stream = response.GetResponseStream())
{
var result = stream != null ? new StreamReader(stream).ReadToEnd() : null;
Global.Logger.Debug("GoogleDriveApp: save file stream response - " + result);
}
}
catch (WebException e)
{
Global.Logger.Error("GoogleDriveApp: Error save file stream", e);
request.Abort();
var httpResponse = (HttpWebResponse)e.Response;
if (httpResponse.StatusCode == HttpStatusCode.Forbidden)
{
throw new SecurityException(FilesCommonResource.ErrorMassage_SecurityException, e);
}
throw;
}
}
}