Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# .ashx-远程服务器返回错误:(500)内部服务器错误_C#_.net_Httpwebrequest_Httpwebresponse_Ashx - Fatal编程技术网

C# .ashx-远程服务器返回错误:(500)内部服务器错误

C# .ashx-远程服务器返回错误:(500)内部服务器错误,c#,.net,httpwebrequest,httpwebresponse,ashx,C#,.net,Httpwebrequest,Httpwebresponse,Ashx,我在一个方法中一个接一个地调用一个远程REST服务。我正在第一次调用中设置accessToken的值,并将其用于第二次请求 当我运行它时,它会给我错误信息 远程服务器返回错误:500内部服务器错误 下面是代码 HttpWebRequest webRequest = null; HttpWebResponse webResponse = null; Encoding encodingObj = null; StreamReader streamReaderObj =

我在一个方法中一个接一个地调用一个远程REST服务。我正在第一次调用中设置accessToken的值,并将其用于第二次请求

当我运行它时,它会给我错误信息

远程服务器返回错误:500内部服务器错误

下面是代码

    HttpWebRequest webRequest = null;
    HttpWebResponse webResponse = null;
    Encoding encodingObj = null;
    StreamReader streamReaderObj = null;

    string grantCode = string.Empty;
    string resultString = string.Empty;
    string accessToken = string.Empty;

    private void Instantiate()
    {            
        grantCode = HttpContext.Current.Request.QueryString["code"].ToString();
        webRequest = (HttpWebRequest)WebRequest.Create(Constants.ACCESS_TOKEN_REQUEST + "&code=" + grantCode);
        webRequest.Method = "GET";
        webRequest.ContentType = "application/json";
        webResponse = (HttpWebResponse)webRequest.GetResponse();
        encodingObj = System.Text.Encoding.GetEncoding("utf-8");
        streamReaderObj = new StreamReader(webResponse.GetResponseStream(), encodingObj);
        resultString = streamReaderObj.ReadToEnd();
        JObject parameterCollection = JObject.Parse(resultString);
        accessToken = parameterCollection["access_token"].ToString();
        //HttpContext.Current.Response.Write("<br/><br/>Code: <br/>" + grantCode);
        //HttpContext.Current.Response.Write("<br/><br/>Access Token: <br/>" + accessToken);

        webRequest = (HttpWebRequest)WebRequest.Create(Constants.RETRIEVE_CONTEXT_REQUEST + "vista-688/id/Staff01");
        webRequest.Method = "GET";
        webRequest.Accept = "application/json";
        webRequest.ContentType = "application/json";
        webRequest.Headers.Add("Authorization", "Bearer " + accessToken);
        webResponse = (HttpWebResponse)webRequest.GetResponse();
        encodingObj = System.Text.Encoding.GetEncoding("utf-8");
        streamReaderObj = new StreamReader(webResponse.GetResponseStream(), encodingObj);
        resultString = streamReaderObj.ReadToEnd();
        //HttpContext.Current.Response.Write("<br/><br/>Retrieve Context: <br/>" + resultString);
    }
以下是配置文件中的完整rest api URL:

<add key="GrantCodeRequest" value="https://<location>/AuthorizationServices/provider/authorize?response_type=code&state=mystateid&client_id=mVisum&redirect_uri=http://localhost:1316/RetrieveContext.aspx&scope=read"/>
<add key="AccessTokenRequest" value="https://<location>/AuthorizationServices/oauth/token?client_id=mVisum&state=mystateid&scope=read&client_secret=TESTMVISUM&response_type=token&grant_type=authorization_code&redirect_uri=http://localhost:1316/RetrieveContext.aspx"/>
<add key="RetrieveContextRequest" value="http://<location>/UserContext/rest/context/user/system/"/>
当我只执行第二个请求,并且accessToken值初始化为有效值时,第二个调用也可以正常工作。此方法在一个处理程序中编写

有谁能告诉我为什么会这样?RESTWeb服务中没有问题。我也尝试过使用两个单独的web请求和web响应对象,但没有任何效果

        WebClient client = new WebClient();
        client.Headers["Content-type"] = @"application/json";
        Stream data = client.OpenRead(yoururl); ;
        StreamReader reader = new StreamReader(data);
        string responseFromServer = reader.ReadToEnd();

以上操作对我来说很好。

所以浏览器显示了500个错误,但是代码中出现了什么异常?您是否尝试过使用try/catch块捕获任何异常?调试时,rservice的完整url是多少?@Cory yes。它只是显示了相同的错误。没有内部异常。@kostasch。请检查编辑后的问题,查看完整的url,无url