开发者

forbidden:403 error while using wcf restful service

开发者 https://www.devze.com 2023-02-16 12:24 出处:网络
I am trying to consume wcf restful service. The code is as follows: private static string SendRequest(string uri, string method, string contentType, string body)

I am trying to consume wcf restful service. The code is as follows:

private static string SendRequest(string uri, string method, string contentType, string body)
        {
            string responseBody = null;

            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(uri);
            req.Method = method;
            if (!String.IsNullOrEm开发者_高级运维pty(contentType))
            {
                req.ContentType = contentType;
            }
            if (body != null)
            {
                byte[] bodyBytes = Encoding.UTF8.GetBytes(body);
                req.GetRequestStream().Write(bodyBytes, 0, bodyBytes.Length);
                req.GetRequestStream().Close();
            }
            req.Accept = "*/*"; 
            HttpWebResponse resp;

            resp = (HttpWebResponse)req.GetResponse();

            return responseBody;
        }

Now the issue is, sometimes it works fine and sometimes i get the error

"the remote server returned an error 403 forbidden."

I cannot figure out why it fails. Any idea???

0

精彩评论

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