.ashx-远程服务器返回错误:(500)内部服务器错误
本文关键字:服务器 错误 内部 返回 ashx- | 更新日期: 2023-09-27 18:25:52
我在单个方法中一个接一个地对一个远程REST service
进行两次调用。我在第一次调用中设置accessToken
的值,并将其用于第二次请求。
当我运行它时,它会给我错误作为
The remote server returned an error: (500) Internal Server Error.
以下是代码。
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
值初始化为有效值的第二个请求时,第二个调用也在正常工作。这个方法是在一个处理程序中编写的。
有人能告诉我为什么会发生这种事吗?REST web服务中没有问题。我也尝试过使用两个独立的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();
以上对我来说很好。