J'utilise jQuery pour obtenir des données d'une API.401 Webmethod non autorisée uniquement sur le serveur
Le lecteur de flux authentifie les appels à l'api et obtient le flux comme ceci:
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;
}
}
Mes services se présentent comme suit:
[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");
}
}
Et l'un des appels ajax jQuery comme un exemple ressemble à ceci:
// 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);
}
}
});
Tout fonctionne bien localement. quand je télécharge les choses au serveur distant je reçois:
{"Message":"There was an error processing the request.","StackTrace":"","ExceptionType":""}
GET http://hci.me.uk/police/PoliceApi.asmx/requestLastTimeUpdated
401 Unauthorized
Avant de faire les services asmx, je les avais utilisés par ASPX bien que cela a causé un certain problèmes de performances et de sérialisation, il fonctionnait correctement pour certains services. L'API nécessite une authentification pour que toutes les demandes get fonctionnent.
Avant de faire les services asmx, je les avais utilisés par ASPX mais cela a causé des problèmes en ce qui concerne les performances et la sérialisation. Cependant, il fonctionnait bien pour certains services. L'API nécessite une authentification pour que toutes les demandes get fonctionnent. – XGreen
Ok. Réponse mise à jour, essayez # 1 – BumbleB2na
BumbleB2na vous êtes l'homme! ;) – XGreen