Using a custom value for HttpMethod

烈酒焚心 提交于 2020-01-03 15:22:26

问题


I'm using the HttpClient and I need to set a non-standard type for the HttpMethod. Where using HttpWebRequest only expects a string, HttpClient expects an HttpMethod. Enumerating the available values in HttpMethod, I don't see a way to add a custom one. Any thoughts?


回答1:


Don't know why I didn't think of trying this before, but I can call new HttpMethod("MYMETHOD");




回答2:


Thank you so much. I tried the weirdest stuff, but i did not see this simple solution :)

I try to use CalDAV REST-Requests to connect with my ownCloud-CalDAV-Server within a XAML-C#-MetroApp. Works perfect now. I finally can change the HTTP-Method to the PROPFIND-Type.

Here my Code for retrieving Infos ( http://sabre.io/dav/building-a-caldav-client/ ).

    ...
        try {
            HttpClientHandler httpClientHandler = new HttpClientHandler();
            httpClientHandler.AllowAutoRedirect = false;
            httpClientHandler.Credentials = new NetworkCredential(caldavuserTB.Text, caldavpasswordTB.Text);

            HttpClient httpClient = new HttpClient(httpClientHandler);
            httpClient.MaxResponseContentBufferSize = 256000;

            propfindMethod = new HttpMethod("PROPFIND");

            propfindHttpRequestMessage = new HttpRequestMessage(propfindMethod, webURLAsURI);

            propfindHttpRequestMessage.Headers.Add("Prefer", "return-minimal");
            propfindHttpRequestMessage.Headers.Add("Depth", "0");
            propfindHttpRequestMessage.Headers.Add("Accept", "application/xml; charset=utf-8");
            propfindHttpRequestMessage.Content = new StringContent("<d:propfind xmlns:d=\"DAV:\" xmlns:cs=\"http://calendarserver.org/ns/\"><d:prop><d:displayname /><cs:getctag /></d:prop></d:propfind>");


            propfindHttpResponseMesage = await httpClient.SendAsync(propfindHttpRequestMessage);
        }
    ...


来源:https://stackoverflow.com/questions/19596386/using-a-custom-value-for-httpmethod

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!