C#:尝试使用httpwebrequest将文件发送到自托管的Web服务时出错

时间:2014-07-01 19:10:18

标签: c# post webmethod self-hosting .net-cf-3.5

首先,我的服务器是一个自托管的c#webservice。它适用于我的其他应用程序,但现在我必须在其中实现一个将由Windows CE 5的移动计算机使用的方法。我需要在我的服务器中接收一个文件,所以我写了这个方法:

[System.ServiceModel.OperationContract, WebInvoke(UriTemplate = "test/")]
string testSaveFile(Stream arq);


public string testSaveFile(Stream img) {
    Console.WriteLine("stream received");
    Console.WriteLine("stream size: " + img.Length); //throws "Error 400 Bad Request"
}

在移动计算机上,使用.Net CF 3.5我已经写了这个方法来发送文件:

public static string UploadFilesToRemoteUrl( string url, string file ) {
    long length = 0;
    string boundary = "----------------------------" +
            DateTime.Now.Ticks.ToString( "x" );
    HttpWebRequest httpWebRequest2 = ( HttpWebRequest ) WebRequest.Create( url );

    httpWebRequest2.AllowWriteStreamBuffering = true;
    httpWebRequest2.ContentType = "multipart/form-data; boundary=" + boundary;
    httpWebRequest2.Method = "POST";
    httpWebRequest2.KeepAlive = true;
    httpWebRequest2.Credentials =
    System.Net.CredentialCache.DefaultCredentials;
    Stream memStream = new System.IO.MemoryStream();
    byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes( "\r\n--" 
        + boundary + "\r\n" );
    memStream.Write( boundarybytes, 0, boundarybytes.Length );
    length += boundarybytes.Length;
    string headerTemplate = "Content-Disposition: form-data; name=\"file\";"
        +" filename=\"file\"\r\n Content-Type: \"application/octet-stream\"\r\n\r\n";
    string header = headerTemplate;
    byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes( header );
    memStream.Write( headerbytes, 0, headerbytes.Length );
    length += headerbytes.Length;
    FileStream fileStream = new FileStream( file, FileMode.Open,
    FileAccess.Read );
    byte[] buffer = new byte[1024];
    int bytesRead = 0;
    while( ( bytesRead = fileStream.Read( buffer, 0, buffer.Length ) ) != 0 ) {
        memStream.Write( buffer, 0, bytesRead );
        length += bytesRead;
    }
    memStream.Write( boundarybytes, 0, boundarybytes.Length );
    length += boundarybytes.Length;
    fileStream.Close();
    httpWebRequest2.ContentLength = memStream.Length;
    Stream requestStream = httpWebRequest2.GetRequestStream();
    memStream.Position = 0;
    byte[] tempBuffer = new byte[memStream.Length];
    memStream.Read( tempBuffer, 0, tempBuffer.Length );
    memStream.Close();
    requestStream.Write( tempBuffer, 0, tempBuffer.Length );
    requestStream.Close();
    WebResponse webResponse2 = httpWebRequest2.GetResponse();
        // ^ Exception here
    Stream stream2 = webResponse2.GetResponseStream();
    StreamReader reader2 = new StreamReader( stream2 );
    string res = reader2.ReadToEnd();
    webResponse2.Close();
    httpWebRequest2 = null;
    webResponse2 = null;

    return res;
}

使用此方法我可以连接到服务器。它甚至显示第一条消息("流接收")。但每当我尝试访问流时,服务器都会返回"错误400错误请求",这在移动计算机上显示为例外。 那么如何将文件从移动计算机发送到服务器?我可以修复此方法还是应该尝试其他方法?

this question的回答说是异步执行,但是我做了并且返回了相同的错误,并且Android应用程序使用同一个服务器同步执行它并且它可以工作。

提前致谢。

另一件事:multipart / form-data是将文件发送到此web方法的正确方法吗?

1 个答案:

答案 0 :(得分:0)

问题不在于发送部分。问题是阅读流长度。 this question的答案帮助了我。