上传文件时,ASP.NET Web服务抛出System.Net.WebException

时间:2011-09-24 10:56:15

标签: c# asp.net

我在asp.net中有一个需要将文件上传到另一台服务器的Web服务。当我在Visual Studio中的调试模式下播放时,代码运行良好。但是当我在IIS中托管并调用它时,它会抛出System.Net.WebException。这是我的代码:

    [WebMethod]
    public string UploadFileToServer(string m_fileName, string m_username, string m_url)
    {
        string m_localFilePath = _currentDirectory + "\\" + m_username + "\\" + m_fileName;
        FileStream m_localFileStream = File.Open(m_localFilePath, FileMode.Open, FileAccess.Read);
        var reader = new BinaryReader(m_localFileStream);

        byte[] m_rawBytes = new byte[m_localFileStream.Length];
        reader.Read(m_rawBytes, 0, m_rawBytes.Length);
        try
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(m_url);
            request.Method = "POST";
            request.KeepAlive = false;

            Stream m_requestStream = request.GetRequestStream();
            m_requestStream.Write(m_rawBytes, 0, m_rawBytes.Length);
            m_requestStream.Close();
            m_localFileStream.Close();

            HttpWebResponse m_response = (HttpWebResponse)request.GetResponse();
            StreamReader m_reader = new StreamReader(m_response.GetResponseStream());

            string s = m_reader.ReadToEnd();

            return s;
        }
        catch (Exception ex)
        {
            return ex.ToString() + ex.Message;
        }
    }

对我来说非常沮丧,并且不可能一直在我的Visual Studio上打开它。有什么建议或解决方案吗?感谢。

0 个答案:

没有答案