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
| string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");
string endpoint = "http://.../nuxeo/restAPI/default/a041a81b-8fa1-465c-88d3-3a89cbd64e2a/yopla/uploadFile";
FileStream rdr = new FileStream(@"C:\test\test.pdf", FileMode.Open);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(endpoint);
req.Credentials = new NetworkCredential("Administrator", "Administrator");
req.Method = "POST";
req.ContentType = "multipart/form-data; boundary="+boundary;
req.ContentLength = rdr.Length;
req.AllowWriteStreamBuffering = true;
//req.KeepAlive = false;
Stream reqStream = req.GetRequestStream();
byte[] inData = new byte[rdr.Length];
int bytesRead = rdr.Read(inData, 0, (int)rdr.Length);
// put data into request stream
reqStream.Write(inData, 0, (int)rdr.Length);
rdr.Close();
// after uploading close stream
reqStream.Close();
public string GetMessage(HttpWebRequest request)
{
using (var response = (HttpWebResponse)request.GetResponse())
{
var responseValue = string.Empty;
if (response.StatusCode != HttpStatusCode.OK && response.StatusCode != HttpStatusCode.Created)
{
string message = String.Format("POST failed. Received HTTP {0}", response.StatusCode);
throw new ApplicationException(message);
}
// grab the response
using (var responseStream = response.GetResponseStream())
{
using (var reader = new StreamReader(responseStream))
{
responseValue = reader.ReadToEnd();
}
}
return responseValue;
}
} |
Partager