从c#类调用json时权限被拒绝

时间:2012-12-13 03:46:20

标签: c# json

我正在尝试从json类库调用c#,下面是我正在使用的代码。在浏览器中访问json网址时,我可以看到相应的数据。但是从代码中可以看出您没有权限调用此网址

请帮助您解决此错误。

程序

URL:

string url = https://landfill.bugzilla.org/bugzilla-tip/jsonrpc.cgi?method=Product.get&params= [{“ids”:“4”}]

try
{
    string ret = string.Empty;
    StreamWriter requestWriter;
    var webRequest = System.Net.WebRequest.Create(url) as HttpWebRequest;
    if (webRequest != null)
    {
        webRequest.Method = "POST";
        webRequest.ServicePoint.Expect100Continue = false;
        webRequest.Timeout = 20000;

        webRequest.ContentType = "application/json";
        //POST the data.
        using (requestWriter = new StreamWriter(webRequest.GetRequestStream()))
        {
            requestWriter.Write(postData);
        }
    }
    HttpWebResponse resp = (HttpWebResponse)webRequest.GetResponse();
    Stream resStream = resp.GetResponseStream();
    StreamReader reader = new StreamReader(resStream);
    ret = reader.ReadToEnd();
    return ret;
}
catch (WebException exception)
{
    string responseText;
    using (var reader = new StreamReader(exception.Response.GetResponseStream()))
    {
        responseText = reader.ReadToEnd();
    }
    return responseText;
}

1 个答案:

答案 0 :(得分:0)

你正在做“GET”而非POST吗?

改变这个:

webRequest.Method = "POST";

webRequest.Method = "GET";