2011-06-24 23 views
0

jQueryを使用して、APIからデータを取得しています。401サーバー上でのみ許可されていないWebメソッド

ストリームリーダは、APIの呼び出しを認証し、このようにストリームを取得します。例として

[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呼び出しの1:

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; 
    } 
} 

は私のサービスは、次のようになり

// 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":""} 

は前ASMXサービスを作ることにhttp://hci.me.uk/police/PoliceApi.asmx/requestLastTimeUpdated

401権限

をGETこれは、いくつかの原因となったが、私は、ASPXを経由して、それらを使用されていましたパフォーマンスとシリアライゼーションに関する問題は、一部のサービスでうまく機能していました。 APIは、すべてのget要求が機能するための認証を要求します。

Demo link to this app

答えて

1

1)、私はあなたのWebサービスをテストしようとすると、それは私に語った:放置しないでください:

警告「テストフォームは、ローカルコンピュータからの要求に対してのみ使用可能です」

:あなたがローカルホストの外でWebサービスをテストすることができるように

をテストし、あなたが行われた後にこのようなあなたのweb.configファイルには、web.configファイルにして、これを追加します0

<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)PoliceAPIUsernamePoliceAPIPassword がのAppSettingsにweb.configファイル

3のローカルのバージョンと同じに格納されていることを、あなたのライブweb.configファイルをダブルチェック)あなたからのデータを要求しているかもしれないAPIライブWebサービスに匿名認証が必要です。私は、匿名ユーザーはローカルでテストするときにデフォルトで許可されていると思います。

this article私はあなたの問題と思われるものに関連しています。この問題は発生して他の誰のために

+0

、しかし私は、これはパフォーマンスおよび直列化に関するいくつかの問題を引き起こしていたのaspxを経由して、それらを使用されていました。しかし、それはいくつかのサービスでうまく動作していました。 APIは、すべてのget要求が機能するための認証を要求します。 – XGreen

+0

よろしくお願いします。答えが更新されました。#1 – BumbleB2na

+0

BumbleB2naあなたは男です! ;) – XGreen

1

- あなたがウェブからのWebサービスを呼び出すことができることを確認するには、この行のコメントを解除してくださいを...

[System.Web.Script.Services。ScriptService】従来ASMXサービスを作るに

乾杯

関連する問題