Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Платформы Windows Mobile и Windows Embedded > Отправка Файла на сервер


Автор: ishimcity 21.1.2009, 10:53
Здраствуйте ребята! Реализую код для отправки файла на сервер. все нормально компилируется для КПК но при запуске на девайся выкидвает ошибку ProtocolViolationException с чем это связано?спасибо

Код

public static string uploadfileex( string uploadfile, string url, 
    string fileformname, string contenttype,namevaluecollection querystring )
{
    if( (fileformname== null) ||
        (fileformname.length ==0))
    {
        fileformname = "file";
    }

    if( (contenttype== null) ||
        (contenttype.length ==0))
    {
        contenttype = "application/octet-stream";
    }


    string postdata;
    postdata = "?";
    if (querystring!=null)
    {
        foreach(string key in querystring.keys)
        {
            postdata+= key +"=" + querystring.get(key)+"&";
        }
    }
    uri uri = new uri(url+postdata);


    string boundary = "----------" + datetime.now.ticks.tostring("x");
    httpwebrequest webrequest = (httpwebrequest)webrequest.create(uri);
    webrequest.cookiecontainer = cookies;
    webrequest.contenttype = "multipart/form-data; boundary=" + boundary;
    webrequest.method = "post";


    // build up the post message header

    stringbuilder sb = new stringbuilder();
    sb.append("--");
    sb.append(boundary);
    sb.append("\r\n");
    sb.append("content-disposition: form-data; name=\"");
    sb.append(fileformname);
    sb.append("\"; filename=\"");
    sb.append(path.getfilename(uploadfile));
    sb.append("\"");
    sb.append("\r\n");
    sb.append("content-type: ");
    sb.append(contenttype);
    sb.append("\r\n");
    sb.append("\r\n");            

    string postheader = sb.tostring();
    byte[] postheaderbytes = encoding.utf8.getbytes(postheader);

    // build the trailing boundary string as a byte array

    // ensuring the boundary appears on a line by itself

    byte[] boundarybytes = 
           encoding.ascii.getbytes("\r\n--" + boundary + "\r\n");

    filestream filestream = new filestream(uploadfile, 
                                filemode.open, fileaccess.read);
    long length = postheaderbytes.length + filestream.length + 
                                           boundarybytes.length;
    webrequest.contentlength = length;

    stream requeststream = webrequest.getrequeststream();

    // write out our post header

    requeststream.write(postheaderbytes, 0, postheaderbytes.length);

    // write out the file contents

    byte[] buffer = new byte[checked((uint)math.min(4096, 
                             (int)filestream.length))];
    int bytesread = 0;
    while ( (bytesread = filestream.read(buffer, 0, buffer.length)) != 0 )
        requeststream.write(buffer, 0, bytesread);

    // write out the trailing boundary

    requeststream.write(boundarybytes, 0, boundarybytes.length);
    webresponse responce = webrequest.getresponse();
    stream s = responce.getresponsestream();
    streamreader sr = new streamreader(s);

    return sr.readtoend();
}



Код

uploadfile = "c:\\test.doc";
string outdata = UploadFileEx(uploadfile,"http://localhost/test.php","file", "application/octet-stream", 
     querystring);



В чем косяк? голову сломал уже

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)