禁止:403使用wcf restful服务时出错
我正在尝试使用wcf restful服务。代码如下:禁止:403使用wcf restful服务时出错,wcf,http-status-code-403,wcf-rest,Wcf,Http Status Code 403,Wcf Rest,我正在尝试使用wcf restful服务。代码如下: private static string SendRequest(string uri, string method, string contentType, string body) { string responseBody = null; HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(uri);
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.IsNullOrEmpty(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;
}
现在的问题是,有时它工作正常,有时我会出错
"the remote server returned an error 403 forbidden."
我不明白为什么它失败了。你知道吗?你一直在请求同一个URI(当你收到错误时)。您能否尝试捕获异常并检查是否存在任何子状态(例如403.14,目录列表被拒绝)?我认为你需要收集更多的信息来解决这个问题。回复文本说,“底层连接已关闭”,那么托管服务呢?你能控制它吗?它在哪里托管?凭什么?