开发者

Send POST request for REST api method call

开发者 https://www.devze.com 2023-03-19 09:25 出处:网络
I have a problem with sending POST http request. It stops on (HttpWebResponse)request.GetResponse() and after timeout throws timeout expired exception, but if i send the same request via GET all works

I have a problem with sending POST http request. It stops on (HttpWebResponse)request.GetResponse() and after timeout throws timeout expired exception, but if i send the same request via GET all works fine.

Does any body know what it can be?

try
        {
            var request = (HttpWebRequest)WebRequest.C开发者_运维知识库reate(uri);
            request.Method = "POST";

            if (content != null)
                request.GetRequestStream().Write(content, 0, content.Length);

            using (var response = (HttpWebResponse)request.GetResponse())
            {
                return new Response(response);
            }
        }
        catch (WebException exception)
        {
            return new Response(exception);
        }


Most likely, this is due to the code on the server not exposing this method as a POST. If the server doesn't explicitly set anyhting, it defaults to GET only.


Fixed problem with this code:

        using (var requestStream = request.GetRequestStream())
        {
            if (content != null)
            {
                requestStream.Write(content, 0, content.Length);
            }

            requestStream.Close();

            using (var response = (HttpWebResponse)request.GetResponse())
            {
                return new Response(response);
            }
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号