I am using jQuery to get some data from an API.
The stream reader authenticates the calls to the api and gets the stream like this:
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;
}
}
My Services look like this:
[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");
}
}
And one of the jQuery ajax calls as an example looks like this:
// 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);
}
}
});
Everything works fine locally. when I upload the stuff to the remote server I get:
{"Message":"There was an error processing the request.","StackTrace":"","ExceptionType":""}
GET http://hci.me.uk/police/PoliceApi.asmx/requestLastTimeUpdated
401 Unauthorized
Prior to making the asmx services, I had them being used via aspx although this was causing some issues regarding performance and serialization, it used to work fine for some of the services. The API requires authentication for all get requests to work.
Demo link to this app
1) When, I try to test your webservice it tells me: "The test form is only available for requests from the local machine"
Warning: Don't leave your web.config like this after you are done testing
Add this in to web.config so you can test the webservice outside of localhost:
<configuration>
<system.web>
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
</system.web>
</configuration>
Then, go here to test: http://hci.me.uk/police/PoliceApi.asmx?op=requestLastTimeUpdated
After testing, remove those lines from web.config for security reasons.
2) Double-check your live web.config that the PoliceAPIUsername
and PoliceAPIPassword
are stored in AppSettings the same as in your local version of web.config
3) Maybe the API you are requesting data from needs anonymous authentication to your live web services. I think anonymous users are allowed by default when testing locally.
I found this article related to what I think might be your problem.
For anyone else experiencing this issue - make sure you uncomment this line to ensure you can call the web service from the web...
[System.Web.Script.Services.ScriptService]
Cheers
精彩评论