使用Web API 2.0和httpwebrequest的REST httppost

时间:2018-08-06 09:07:32

标签: c# asp.net-web-api2

我正在进行服务器到服务器的调用,即浏览器调用了我用C#编写的帖子,并且可以看到用户上传的文件。通过此post方法,我将向另一个服务器发送另一个httppost,但是在此post方法中,我无法获取文件。

foreach (string file in httpRequest.Files)
{
    var postedFile = httpRequest.Files[file];
    fileContent = postedFile.InputStream;

    byte[] data;
    using (Stream inputStream = fileContent)
    {
        MemoryStream memoryStream = inputStream as MemoryStream;
        if (memoryStream == null)
        {
            memoryStream = new MemoryStream();
            inputStream.CopyTo(memoryStream);
        }
        data = memoryStream.ToArray();
    }

    string InternalRESTURL = ConfigurationManager.AppSettings["InternalRESTURL"];

    InternalRESTURL = InternalRESTURL + "PostImageToStorage?clientId=" + clientId + "&configId=" + configId;
    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(InternalRESTURL);
    webRequest.Method = "POST";
    webRequest.ContentType = "multipart/form-data";

    using (var stream = webRequest.GetRequestStream())
    {
        stream.Write(data, 0, data.Length);
    }

    try { 
        var response = (HttpWebResponse)webRequest.GetResponse();
    }
    catch(Exception ex)
    {

    }
}

调试上面的代码时,我可以在filecontent对象中看到lengthhttpwebrequest

下面是InternalRESTURL post方法,在这里我无法获取上述代码发送的文件:

public HttpResponseMessage PostImageToStorage(int clientId, int configId)
{
    System.Diagnostics.Debugger.Launch();
    HttpResponseMessage result = Request.CreateResponse(HttpStatusCode.BadRequest);
    Stream fileStreamContent = null;
    var httpRequest = System.Web.HttpContext.Current.Request;
    HttpFileCollection temp = httpRequest.Files;
    int filecount = temp.Count;
    var filePath = "C:\\temp\\" + httpRequest.Headers["filename"];
            _logger.Info("filecount: " + filecount.ToString());
            _logger.Info("filePath: " + filePath.ToString());
    foreach (string file in httpRequest.Files)
    {
        var postedFile = httpRequest.Files[file];
        fileStreamContent = postedFile.InputStream;

        string uploadedFileName = postedFile.FileName.ToLower();
        string uploadedFileExtn = uploadedFileName.Substring(uploadedFileName.LastIndexOf("."));
    }

    var request = this.Request;
    var provider = new MultipartMemoryStreamProvider();
    string fileName= string.Empty;
    var task = request.Content.ReadAsMultipartAsync(provider).
        ContinueWith(o =>
        {
            //Select the appropriate content item this assumes only 1 part
            var fileContent = provider.Contents.SingleOrDefault();

            if (fileContent != null)
            {
                fileName = fileContent.Headers.ContentDisposition.FileName.Replace("\"", string.Empty);
            }
        });
    _logger.Info("filename: "+fileName);
    foreach (var file in provider.Contents)
    {
        var filename = file.Headers.ContentDisposition.FileName.Trim('\"');
        var buffer = file.ReadAsByteArrayAsync();
        //Do whatever you want with filename and its binaray data.
    }

    return result;
}

我尝试了两种读取文件的方法,但是我看不到第二个post方法接收到文件。有人可以建议吗?

0 个答案:

没有答案