2011-06-24 58 views
0

我正在使用jQuery從API獲取一些數據。401未經授權的Webmethod只在服務器上

流閱讀器驗證到API的調用,並得到這樣的流:

public string StreamManagerUrlHandler(string requestUrl) 
{ 
    try 
    { 
     Uri reUrl = new Uri(requestUrl); 
     WebRequest webRequest; 
     WebResponse webResponse; 

     webRequest = HttpWebRequest.Create(reUrl) as HttpWebRequest; 
     webRequest.Method = WebRequestMethods.Http.Get; 
     webRequest.ContentType = "application/x-www-form-urlencoded"; 
     Encoding encode = System.Text.Encoding.GetEncoding("utf-8"); 

     webRequest.Credentials = new NetworkCredential(
       ConfigurationManager.AppSettings["PoliceAPIUsername"].ToString(), 
       ConfigurationManager.AppSettings["PoliceAPIPassword"].ToString()); 

     // Return the response. 
     webResponse = webRequest.GetResponse(); 

     using (StreamReader reader = new StreamReader(webResponse.GetResponseStream(), encode)) 
     { 
      string results = reader.ReadToEnd(); 
      reader.Close(); 
      webResponse.Close(); 
      return results; 
     } 
    } 
    catch (Exception e) 
    { 
     return e.Message; 
    } 
} 

我的服務是這樣的:

[WebService(Namespace = "http://tempuri.org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
//[System.Web.Script.Services.ScriptService] 
[ScriptService()] 
public class PoliceApi : System.Web.Services.WebService { 

    public PoliceApi() { 

     //Uncomment the following line if using designed components 
     //InitializeComponent(); 
    } 

    [WebMethod(true)] 
    [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)] 
    public string requestLocalCrime(string lat, string lng) 
    { 
     StreamManager streamMan = new StreamManager(); 
     return streamMan.StreamManagerUrlHandler("http://policeapi2.rkh.co.uk/api/crimes-street/all-crime?lat=" + lat + "&lng=" + lng + ""); 
    } 

    // Method for getting the data database was Last updated 
    [WebMethod(true)] 
    [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)] 
    public String requestLastTimeUpdated() 
    { 
     StreamManager streamMan = new StreamManager(); 
     return streamMan.StreamManagerUrlHandler("http://policeapi2.rkh.co.uk/api/crime-last-updated"); 
    } 

    // Method for getting the data database was Last updated 
    [WebMethod(true)] 
    [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)] 
    public String locateNeighbourhood(string lat, string lng) 
    { 
     StreamManager streamMan = new StreamManager(); 
     return streamMan.StreamManagerUrlHandler("http://policeapi2.rkh.co.uk/api/locate-neighbourhood?q=" + lat + "%2C" + lng + ""); 
    } 

    [WebMethod(true)] 
    [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)] 
    public string neighbourhoodTeam(string force, string neighbourhood) 
    { 
     StreamManager streamMan = new StreamManager(); 
     return streamMan.StreamManagerUrlHandler("http://policeapi2.rkh.co.uk/api/" + force + "%2F" + neighbourhood + "%2F" + "people"); 
    } 
} 

而jQuery的Ajax調用作爲例子之一看起來像這樣:

// Getting last time the API data was updated 
$.ajax({ 
    type: "GET", 
    contentType: "application/json; charset=utf-8", 
    url: "../police/PoliceApi.asmx/requestLastTimeUpdated", 
    dataType: "json", 
    success: function (data) { 
     PoliceApp.mapForm.data('lastupdated', $.parseJSON(data.d).date); 
    }, 
    error: function (res, status) { 
      if (status === "error") { 
       // errorMessage can be an object with 3 string properties: ExceptionType, Message and StackTrace 
       var errorMessage = $.parseJSON(res.responseText); 
       alert(errorMessage.Message); 
      } 
     } 
}); 

一切工作正常本地。當我上傳的東西到遠程服務器,我得到:

{"Message":"There was an error processing the request.","StackTrace":"","ExceptionType":""} 

GET http://hci.me.uk/police/PoliceApi.asmx/requestLastTimeUpdated

401未授權

此前使得ASMX服務,我讓他們經由ASPX使用,儘管這是導致一些關於性能和序列化的問題,它過去對一些服務工作正常。 API需要驗證所有獲取請求的工作。

Demo link to this app

回答

1

1)的時候,我嘗試測試您的Web服務,它告訴我:「測試窗體只能用於來自本地計算機的請求」

警告:不要離開你的web.config這樣在完成後測試

中添加這web.config文件,所以你可以測試本地主機之外的web服務:

<configuration> 
    <system.web> 
    <webServices> 
     <protocols> 
      <add name="HttpGet"/> 
      <add name="HttpPost"/> 
     </protocols> 
    </webServices> 
    </system.web> 
    </configuration> 

然後,去這裏測試:http://hci.me.uk/police/PoliceApi.asmx?op=requestLastTimeUpdated

測試後,從web.config中刪除那些行出於安全原因。

2)仔細檢查現場的web.config的PoliceAPIUsernamePoliceAPIPassword 存儲在AppSettings的一樣的web.config

3的本地版本)也許API您從請求數據需要對您的實時Web服務進行匿名身份驗證。我認爲匿名用戶在本地測試時默認是允許的。

我發現this article與我認爲可能是您的問題有關。

+0

此前使得ASMX服務,我讓他們經由ASPX使用但是這是導致有關性能和系列化的一些問題。然而,它曾經爲一些服務工作得很好。 API需要驗證所有獲取請求的工作。 – XGreen

+0

好的。更新回答,請嘗試#1 – BumbleB2na

+0

BumbleB2na你是男人! ;) – XGreen

1

對於其他人遇到這個問題 - 確保你去掉這一行,以確保您可以從Web調用Web服務...

[System.Web.Script.Services。ScriptService]

乾杯

相關問題