English 中文(简体)
401 未经许可 只使用服务器上的网络方法
原标题:401 Unauthorized Webmethod only on the server

I am using jQuery to get some data from an API.

溪流的读者验证了向pi子发出的电话,并得出了这样的结果:

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

例如,一个叫作 j的 j子就是:

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

一切都在当地生产。 当我把 st子上载到遥远的服务器时,我得到:

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

GET

401 未经许可

在提供食堂服务之前,我曾经通过牛肉使用过这些器具,尽管这造成了一些关于业绩和序列化的问题,但一些服务却被罚款。 公益物认证要求所有要求工作的人。

rel=“nofollow”>Demolink to this app

最佳回答

(1) 当我试图测试你的网络服务时,我告诉我:。 测试表仅供当地机器要求使用。”

www.un.org/Depts/DGACM/index_spanish.htm 警告: 页: 1 在你进行检测之后,就如:。

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检测,从网上删除这些线。 出于安全原因召集会议

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) 或许是,你要求获得来自需要匿名认证的数据,以证明您的实况网络服务。 我认为,在当地进行测试时,匿名用户可以缺席。

我发现,该条涉及我认为可能是你的问题。

问题回答

对遇到这一问题的其他人来说,确保你不满意这一行,以确保你能够从网络上打电话。

[System.Web.Script.Services.ScriptService]

Cheers





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!