[Mono-bugs] [Bug 418012] New: WebClient.UploadFile = 500 internal server error
bugzilla_noreply at novell.com
bugzilla_noreply at novell.com
Mon Aug 18 10:05:49 EDT 2008
https://bugzilla.novell.com/show_bug.cgi?id=418012
Summary: WebClient.UploadFile = 500 internal server error
Product: Mono: Class Libraries
Version: SVN
Platform: i686
OS/Version: Linux
Status: NEW
Severity: Normal
Priority: P5 - None
Component: System
AssignedTo: mono-bugs at lists.ximian.com
ReportedBy: surfzoid2002 at yahoo.fr
QAContact: mono-bugs at lists.ximian.com
Found By: DeveloperNet
Hi, i'm writing MonoOSC, i use WebClient.UploadFile to upload new files in
OpenSuse build service.
I cannot really provide test case, so you need a valid Novell account and start
a project/pkg at https://build.opensuse.org/
After use this two function , first is really basic and second is a modified
version of the mono class WebClient.UploadFile :
//VarGlobal.OpenSuseApiUrl is : "https://api.opensuse.org/"
//PkgName is "MonoOSC"
//example of FuncAndArgs : "source/home:surfzoid/" + PkgName +
"/MyShortFileName.file"
using System;
using System.Collections.Generic;
using System.Text;
// We use the HttpUtility class from the System.Web namespace
using System.Web;
using System.Net;
using System.IO;
using System.Diagnostics;
using System.Threading;
using System.Globalization;
static public void Putit(string FuncAndArgs, string User, string
Password, string SourceFile)
{
try {
Uri DestFile = new Uri(VarGlobal.OpenSuseApiUrl
+ FuncAndArgs);
WebClient request = new WebClient();
//parametre de connexion
request.Credentials = new NetworkCredential(User, Password);
request.UploadFile(DestFile, "PUT", SourceFile);
} catch (Exception Ex) {
Console.WriteLine(Ex.Message +
Environment.NewLine + Ex.StackTrace);
}
}
static public byte [] UploadFileCore (string FuncAndArgs,
string User, string Password, string SourceFile)
{
Console.WriteLine("UploadFileCore");
try {
Uri address = new Uri(VarGlobal.OpenSuseApiUrl + FuncAndArgs);
// Create the web request
WebRequest request = WebRequest.Create(address) as WebRequest;
// Add authentication to request
request.Credentials = new NetworkCredential(User, Password);
request.Timeout = VarGlobal.TimeOut;
// Set type to PUT
request.Method = "PUT";
string boundary = "---------------------" +
DateTime.Now.Ticks.ToString("x", NumberFormatInfo.InvariantInfo);
string contentType = "multipart/form-data";
string formHeader = "--" + boundary + "\r\n"
+ "Content-Disposition: form-data;
name=\"file\"; filename=\"" + Path.GetFileName(SourceFile) + "\"\r\n"
+ "Content-Type: " + contentType + "\r\n"
+ "\r\n";
byte[] formHeaderBytes = Encoding.UTF8.GetBytes(formHeader);
byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" +
boundary + "--\r\n");
WebHeaderCollection Headers = new WebHeaderCollection();
string fileCType = Headers ["Content-Type"];
if (fileCType != null) {
string lower = fileCType.ToLower ();
if (lower.StartsWith ("multipart/"))
throw new WebException ("Content-Type
cannot be set to a multipart" +
" type for this
request.");
} else {
fileCType = "application/octet-stream";
}
Stream reqStream = null;
Stream fStream = null;
byte [] resultBytes = null;
SourceFile = Path.GetFullPath (SourceFile);
try {
fStream = File.OpenRead (SourceFile);
reqStream = request.GetRequestStream ();
reqStream.Write(boundaryBytes, 0, boundaryBytes.Length);
reqStream.Write(formHeaderBytes, 0, formHeaderBytes.Length);
int nread;
byte [] buffer = new byte [4096];
while ((nread = fStream.Read (buffer, 0, 4096))
!= 0)
reqStream.Write (buffer, 0, nread);
reqStream.Close ();
reqStream = null;
WebResponse response = request.GetResponse ();
StreamReader reader = new
StreamReader(response.GetResponseStream());
resultBytes =
Encoding.UTF8.GetBytes(reader.ReadToEnd().ToString());
}
catch (ThreadInterruptedException Tex)
{
Console.WriteLine(Tex.Message + Environment.NewLine +
Tex.StackTrace);
if (request != null)
request.Abort ();
throw;
} finally {
if (fStream != null)
fStream.Close ();
if (reqStream != null)
reqStream.Close ();
}
return resultBytes;
} catch (Exception Ex) {
Console.WriteLine(Ex.Message +
Environment.NewLine + Ex.StackTrace);
}
return null;
}
--
Configure bugmail: https://bugzilla.novell.com/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.
You are the assignee for the bug.
More information about the mono-bugs
mailing list