2012-12-13 42 views
-1

我想從c#類庫調用json,下面是我正在使用的代碼。在瀏覽器中訪問json網址時,我能夠看到相應的數據。但從代碼它是給你沒有權限調用這個URL從C#調用json時的權限被拒絕類

請幫助如何克服這個錯誤。

計劃

網址:

字符串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; 
} 

回答

0

你正在做一個」GET「而不是POST的權利?

改變這一點:「不能與這個動詞型發送的內容體」

webRequest.Method = "POST"; 

webRequest.Method = "GET"; 
+0

當我改變這讓我越來越喜歡execption – user1899731

+0

現在刪除了發佈數據部分及其正常工作:)非常感謝。 – user1899731

+0

你能告訴我如何在Json中傳遞參數嗎? – user1899731