Hochladen einer Datei auf einen web-service mit POST in C#

Das problem bei diesem code ist, dass die Datei, sobald es hochgeladen wurde, ist nicht das richtige format. Ich bin versucht zu uploaden .zip-Datei.

public string HttpPost(string uri, string Parameters)
{
WebRequest webRequest = WebRequest.Create(uri);

        NetworkCredential credentials = new NetworkCredential("username", "password");
        webRequest.Credentials = credentials;

        webRequest.ContentType = "application/x-www-form-urlencoded";
        webRequest.Method = "POST";

        byte[] bytes = Encoding.ASCII.GetBytes(parameter);
        Stream os = null;
        try
        { //send the Post
            webRequest.ContentLength = bytes.Length;   //Count bytes to send
            os = webRequest.GetRequestStream();
            os.Write(bytes, 0, bytes.Length);         //Send it
        }
        catch (WebException ex)
        {
            MessageBox.Show(ex.Message, "HttpPost: Request error",
               MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        finally
        {
            if (os != null)
            {
                os.Close();
            }
        }

        try
        { //get the response
            WebResponse webResponse = webRequest.GetResponse();
            if (webResponse == null)
            { return null; }
            StreamReader sr = new StreamReader(webResponse.GetResponseStream());
            return sr.ReadToEnd().Trim();
        }
        catch (WebException ex)
        {
            MessageBox.Show(ex.Message, "HttpPost: Response error",
               MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        return null;
    } 
Die web-services-Plattform verwenden Sie? ASMX, WCF (RESTful oder SOAP?), ASP.NET MVC-Web-API, etc?
SDO-REST-API...
Amazon S3......
Sie hochladen möchten, um - Reißverschluss Datei-Inhalt Typ sollte application/octet-stream
was ist mit der Codierung. würde es noch sein, was habe ich? byte[] bytes = Encoding.ASCII.GetBytes(Parameters);

InformationsquelleAutor Rj. | 2012-08-06

Schreibe einen Kommentar