通过HttpWebRequest上传文件"远程服务器返回错误:(405)方法不允许.."

时间:2015-09-08 22:34:53

标签: c# post httpwebrequest webclient webrequest

当我尝试通过post将.xml文件上传到网络服务器时,我遇到了错误。如果我使用浏览器提交表单,则这是标题信息。

常规

Remote Address:10.0.1.90:80
Request URL:http://10.0.1.90/config/
Request Method:POST
Status Code:200 OK

响应标题

Connection:keep-alive
Content-Encoding:gzip
Content-Type:text/html
Date:Thu, 09 Oct 2003 16:25:41 GMT
Server:nginx/1.0.5
Transfer-Encoding:chunked

请求标题

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Authorization:Basic bWFuYWdlcjptYW5hZ2VyMDE=
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:1500
Content-Type:multipart/form-data; boundary=----    WebKitFormBoundaryEr6fyNPJYuClCcHr
Host:10.0.1.90
Origin:http://10.0.1.90
Referer:http://10.0.1.90/config/
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36

请求有效负载

------WebKitFormBoundaryEr6fyNPJYuClCcHr
Content-Disposition: form-data; name="newconfig"; filename="config.xml"
Content-Type: text/xml

------WebKitFormBoundaryEr6fyNPJYuClCcHr
Content-Disposition: form-data; name="put"

Upload
------WebKitFormBoundaryEr6fyNPJYuClCcHr--

这是我使用的c#代码。

        ASCIIEncoding encoder = new ASCIIEncoding();
        byte[] data = encoder.GetBytes(serializedObject); // a json object, or xml, whatever...

        HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
        NetworkCredential up = new NetworkCredential("username", "password");
        request.Credentials = up;
        request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8;";
        request.Method = "POST";
        request.ContentType = "text/xml";
        request.ContentLength = data.Length;


        request.GetRequestStream().Write(data, 0, data.Length);

        HttpWebResponse response = request.GetResponse() as HttpWebResponse;

我不断得到的错误是:"远程服务器返回错误:(405)方法不允许。"

我无法修改运行nginx网站的服务器。

更新的  这是表单html代码,如果它可以帮助任何人帮助我。

<form method="post" enctype="multipart/form-data" action="#">
    <fieldset class="quick_create">
        <input type="submit" name="delete" value="Reset">
        <input type="submit" name="apply" value="Commit">
        <input type="submit" name="apply_reload" value="Commit + Reload">
        <input type="file" name="newconfig">
        <input type="submit" name="put" value="Upload">
    </fieldset>
</form>

第二次更新(使用WebClient.UploadFile)

WebClient webClient = new WebClient();
        try
        {
            webClient.Credentials = new NetworkCredential("user", "pass");
            webClient.Headers["Content-Type"] = "multipart/form-data";
            webClient.Headers["Accept"] = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";

            WebRequest serverRequest = WebRequest.Create(url);
            serverRequest.Credentials = new NetworkCredential("manager", "manager01");

            WebResponse serverResponse;

            serverResponse = serverRequest.GetResponse();
            serverResponse.Close();

            webClient.UploadFile(url, "POST", data);
            webClient.Dispose();
            webClient = null;
        }
        catch (Exception error)
        {
            Console.WriteLine(error.Message);
            Console.ReadLine();
        } 

0 个答案:

没有答案
相关问题