首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

用WebClient上传文件时,提示远程服务器返回异常:(403)已禁止,这个是什么原因

2013-08-27 
用WebClient上传文件时,提示远程服务器返回错误:(403)已禁止,这个是什么原因?private void button4_Click(

用WebClient上传文件时,提示远程服务器返回错误:(403)已禁止,这个是什么原因?
  private void button4_Click(object sender, EventArgs e)
        {
            String sPath = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
            sPath = sPath.Substring(0, sPath.LastIndexOf('\\') + 1);
            Image newImage = Image.FromFile(sPath + "PMimages\\image_HE.png");
            button5.Image = newImage;

            //Stream myStream = null;
            OpenFileDialog dlg = new OpenFileDialog();
            string COMPRESSEDPACKAGE = m_ReadResDLL.ReadString(IDInResDLL.IDS_COMMON_COMPRESSEDPACKAGE);
            dlg.Filter = "RAR(" + COMPRESSEDPACKAGE + ") files (*.rar)|*.rar|All files (*.*)|*.*";
            //dlg.Filter = "RAR(压缩包) files (*.rar)|*.rar|All files (*.*)|*.*";   //中文比较
            dlg.FilterIndex = 1;
            dlg.RestoreDirectory = true;

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    // Insert code to read the stream here.
                    string strFile = dlg.FileName;
                    string strFilename = strFile.Substring(strFile.LastIndexOf("\") + 1);
                    strFilename = strFilename.Substring(0, strFilename.IndexOf("."));



                    /************************************************************************/
                    /*                 xl修改于2010-5-14                                    */
                    //先将文件上传至服务器
                    WebClient myWebClient = new WebClient();
                    // 每次从ini中读取多少字节
                    System.Text.StringBuilder temp = new System.Text.StringBuilder(255);
                    // section=配置节,key=键名,temp=上面,path=路径
                    sPath += "Profile.ini";
                    GetPrivateProfileString("Trans", "ServerIP", "127.0.0.1", temp, 255, sPath);
                    string remoteUri = "http://" + temp.ToString() + "/load/";

                    string myStringWebResource = null;
                    myStringWebResource = remoteUri + strFilename + ".rar";
(每次到这就跳到catch了)  myWebClient.UploadFile(myStringWebResource, "PUT", dlg.FileName);
                    

                    /************************************************************************/
                    int ret = s.ImportPaper(strFilename);



                    //s.DelServerFile(sourpath);
                    if (ret == 0 || ret == -1)
                    {
                        string no = m_ReadResDLL.ReadString(IDInResDLL.IDS_COMMON_IMPORTFAILED);//完成
                        MessageBox.Show(no);
                        //MessageBox.Show("导入失败");
                        return;
                    }
                    string yes = m_ReadResDLL.ReadString(IDInResDLL.IDS_STANDARDDLL_IMPORTSUCCESS);//完成
                    MessageBox.Show(yes);
                    // MessageBox.Show("导入成功");
                    string filename = strFile.Substring(strFile.LastIndexOf("\") + 1);
                    s.DelServerFile(filename);
                    filename = filename.Substring(0, filename.IndexOf("."));
                    s.DelServerFile(filename);

                    // 刷新列表
                    //数据加载显示试卷表的数据
                    DataSet dss = s.SelectPaperAll();
                    DataTable dtt;


                    dtt = dss.Tables[0];
                    dataGridView1.DataSource = dtt;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
            }
        }
[解决办法]
load/文件夹没有设置匿名帐户可写入权限

热点排行