1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
/// <summary>
/// Envoi un fichier de données à BlackBoard
/// renvoi ID du log BB
/// </summary>
private string Push_BB_File(string studentFilePathForBB, string bb_URL, string bb_Login, string bb_Pwd)
{
string dataSetUidLog = "";
HttpWebRequest WRequest;
HttpWebResponse WResponse;
try
{
//ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });
//preAuth the request
// You can add logic so that you only pre-authenticate the very first request.
// You should not have to pre-authenticate each request.
WRequest = (HttpWebRequest)HttpWebRequest.Create(bb_URL);
// Set the username and the password.
WRequest.Credentials = new NetworkCredential(bb_Login, bb_Pwd);
WRequest.PreAuthenticate = true;
WRequest.Method = "HEAD";
WRequest.Timeout = 10000;
WResponse = (HttpWebResponse)WRequest.GetResponse();
WResponse.Close();
// Make the real request
WRequest = (HttpWebRequest)WebRequest.Create(bb_URL);
WRequest.ContentType = "text/plain";
WRequest.Method = "POST";
WRequest.Credentials = new NetworkCredential(bb_Login, bb_Pwd);
WRequest.PreAuthenticate = true;
WRequest.Timeout = 10000;
WRequest.AllowWriteStreamBuffering = false;
FileStream ReadIn = new FileStream(studentFilePathForBB, FileMode.Open, FileAccess.Read);
ReadIn.Seek(0, SeekOrigin.Begin); // Move to the start of the file.
WRequest.ContentLength = ReadIn.Length; // Set the content length header to the size of the file.
Byte[] FileData = new Byte[ReadIn.Length]; // Read the file in 2 KB segments.
int DataRead = 0;
int bufferSize = 2048;
Stream tempStream = WRequest.GetRequestStream();
do
{
DataRead = ReadIn.Read(FileData, 0, bufferSize);
if (DataRead > 0) //we have data
{
tempStream.Write(FileData, 0, DataRead);
Array.Clear(FileData, 0, bufferSize); // Clear the array.
}
} while (DataRead > 0);
using (WResponse = WRequest.GetResponse() as HttpWebResponse)
{
CurrentContext.Message.Display("Content length is {0}", WResponse.ContentLength);
CurrentContext.Message.Display("Content type is {0}", WResponse.ContentType);
if (WResponse.StatusCode == HttpStatusCode.OK)
{
// Get the stream associated with the response.
Stream receiveStream = WResponse.GetResponseStream();
// Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
CurrentContext.Message.Display("Response stream received.");
string responseFromServer = readStream.ReadToEnd();
CurrentContext.Message.Display(responseFromServer);
dataSetUidLog = ExtactBB_DataSetUidLog(responseFromServer);
CurrentContext.Message.Display(dataSetUidLog);
WResponse.Close();
readStream.Close();
}
}
}
catch (WebException ex)
{
using (var stream = ex.Response.GetResponseStream())
using (var reader = new StreamReader(stream))
{
CurrentContext.Message.Display(reader.ReadToEnd());
}
}
catch (Exception ex)
{
// Something more serious happened
// like for example you don't have network access
// we cannot talk about a server exception here as
// the server probably was never reached
}
return dataSetUidLog;
} |
Partager