C# 如何解决Web客户端上传文件“远程服务器返回错误:(405)方法不允许。”?

发布于 2024-11-28 23:03:14 字数 1048 浏览 0 评论 0原文

您好,我想将本地的 html 文件上传到服务器中的远程文件夹,该文件夹包含带有 geoserver 元素的数据目录,这是我的代码:

public void CopyWS(string SourcePath, string DestinationPath)
    {
        try
        {

            string SourcePath = Path.GetFullPath("Result.html");
            string DestinationPath = @"http://xx.xx.xxx.:8080/geoserver/rest/workspaces/";               
            string authInfo = "admin:geoserver";
            WebClient client = new WebClient();
            client.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(authInfo));

          client.UploadFile(DestinationPath, "PUT", SourcePath); 
}

        catch (Exception e)
        {
            MessageBox.Show(e.Message);
        }

我收到以下错误“不允许错误 405 方法”。我尝试使用不同的方法,例如 post 而不是 put,但我遇到了相同的错误。

编辑:有人认为这可能是一个安全问题吗?使用UploadData,我遇到了相同的错误

编辑:经过长时间使用不同方法(UploadDatat即)进行测试后,我总是遇到相同的错误。我一直在搜索和阅读该错误找不到任何真正有用的东西。

编辑:有什么想法吗?

提前致谢

Hello I want to upload a html file that is in my local to a remote folder in a server that contains a data dir with geoserver elements, and here is my code:

public void CopyWS(string SourcePath, string DestinationPath)
    {
        try
        {

            string SourcePath = Path.GetFullPath("Result.html");
            string DestinationPath = @"http://xx.xx.xxx.:8080/geoserver/rest/workspaces/";               
            string authInfo = "admin:geoserver";
            WebClient client = new WebClient();
            client.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(authInfo));

          client.UploadFile(DestinationPath, "PUT", SourcePath); 
}

        catch (Exception e)
        {
            MessageBox.Show(e.Message);
        }

I´m getting the following error "Error 405 method not allowed". I´m trying with different methods like post instead of put but I´m getting the same error.

EDIT: Anybody think that maybe can be a security problem? With UploadData I´m getting the same error

EDIT: After a long time testing with different methods (UploadDatat i.e) I´m getting always the same error.I've been searching and reading around to that and couldn't fine anything really useful.

EDIT: Any idea?

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤独陪着我 2024-12-05 23:03:14

PUT 未配置...通常 PUT(但并非总是)意味着服务器理解 WebDAV...HTTP 上传通常通过 POST 完成...

另一种可能性是某些代理阻止 PUT

编辑 - 根据评论:

POST 请求需要以不同的方式构建,并且取决于服务器期望它们的方式...有关某些示例代码,请参阅 使用 HTTPWebrequest 上传文件(multipart/form-data)

PUT is not configured... usually PUT (but not always) means that the server understands WebDAV... HTTP uploads are usually done via POST...

another possibility would be that some proxy blocks PUT.

EDIT - as per comment:

POST requests need to the be built differentley and depends on how the server expects them... for some sample code see Upload files with HTTPWebrequest (multipart/form-data)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文