RESTful 使用基本身份验证返回错误消息在 C# 中调用
RESTful Call in C# using basic authentication returning error message
我正在尝试调用 OSB 上的 Restful 服务。我的代码是:
string url = _httpGetText + "&$filter=" + filter;
WebRequest request = WebRequest.Create(url);
request.Method = "GET";
ServicePointManager.SecurityProtocol =
SecurityProtocolType.Tls |
SecurityProtocolType.Tls11 |
SecurityProtocolType.Tls12;
request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes("CLLORptngAppIdDev:DPJZuogiYEo5"));
WebResponse response;
try
{
response = request.GetResponse();
List<HistoryRequest> histroyRequestList = LoadHistoryRequest(response);
return histroyRequestList;
}
catch(Exception ex)
{
string msg = ex.Message;
}
捕获中的消息是:远程服务器返回错误:(500) 内部服务器错误。
在服务端,我看到这条消息
<responseSelectedForConsumingProxyService>
<soap-env:Body
xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
<soap-env:Body>
<soap-env:Fault>
<faultcode>Server</faultcode>
<faultstring>This is an Oracle Service Bus generated fault.
[OSB-386420 - A web service security error ocurred while producing security header]
[RouteNode_XXXXXXXXXXXXV_1_0request-pipeline]
[instanceid - *********************************************;]
</faultstring>
</soap-env:Fault>
</soap-env:Body>
</soap-env:Body>
</responseSelectedForConsumingProxyService>
作为评论的结果,我还尝试了以下选项,结果完全相同:
request.Headers["Authorization"] = "Basic " + System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("UTF-8").GetBytes("<ID>:<Pssword>"));
String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes("<ID>:<Pssword>"));
request.Headers.Add("Authorization", "Basic " + encoded);
String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("UTF-8").GetBytes("<ID>:<Pssword>"));
request.Headers.Add("Authorization", "Basic " + encoded);
我想出了以下有效的代码:
public List<HistoryRequest> GetHistoryRequestList()
{
Configuration roamingConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
string OSBBaseUri = ConfigurationManager.AppSettings["OSBUrl"];
string OSBGetEndPoint = ConfigurationManager.AppSettings["OSBEndPoint"];
string MSDCallerId = ConfigurationManager.AppSettings["AppId"];
string Encoding = "iso-8859-1";
string Osbpw = HistoryCrypto.Decrypt(ConfigurationManager.AppSettings["CipherPass"], ConfigurationManager.AppSettings["CryptoKey"]);
CredentialCache credentialCache = new CredentialCache
{
{
new Uri(OSBBaseUri),
"NTLM",
new NetworkCredential()
{
UserName = "<UserName>",
Password = Osbpw,
Domain = ConfigurationManager.AppSettings["Domain"]
}
}
};
HttpMessageHandler handler = new HttpClientHandler()
{
Credentials = credentialCache
};
var _httpClient = new HttpClient(handler)
{
BaseAddress = new Uri(OSBBaseUri),
Timeout = new TimeSpan(0, 2, 0),
};
_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("HTTP_Referrer/HistoryRequestProcess"));
_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("MSCRMCallerID/"+ MSDCallerId));
HttpContent httpContent = new StringContent("{\"operationssubtypecode\":\"HREQ\"} ", System.Text.Encoding.UTF8, "application/json");
var method = new HttpMethod("GET");
var message = new HttpRequestMessage(method, OSBGetEndPoint)
{
Content = httpContent
};
message.Headers.Add("HTTP_Referrer", "HistoryRequestProcess");
message.Headers.Add("MSCRMCallerID", MSDCallerId);
HttpResponseMessage response = _httpClient.GetAsync(OSBGetEndPoint).Result;
string content = string.Empty;
using (StreamReader stream = new StreamReader(response.Content.ReadAsStreamAsync().Result, System.Text.Encoding.GetEncoding(Encoding)))
{
content = stream.ReadToEnd();
}
return LoadHistoryRequest(content);
}
我正在尝试调用 OSB 上的 Restful 服务。我的代码是:
string url = _httpGetText + "&$filter=" + filter;
WebRequest request = WebRequest.Create(url);
request.Method = "GET";
ServicePointManager.SecurityProtocol =
SecurityProtocolType.Tls |
SecurityProtocolType.Tls11 |
SecurityProtocolType.Tls12;
request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes("CLLORptngAppIdDev:DPJZuogiYEo5"));
WebResponse response;
try
{
response = request.GetResponse();
List<HistoryRequest> histroyRequestList = LoadHistoryRequest(response);
return histroyRequestList;
}
catch(Exception ex)
{
string msg = ex.Message;
}
捕获中的消息是:远程服务器返回错误:(500) 内部服务器错误。
在服务端,我看到这条消息
<responseSelectedForConsumingProxyService>
<soap-env:Body
xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
<soap-env:Body>
<soap-env:Fault>
<faultcode>Server</faultcode>
<faultstring>This is an Oracle Service Bus generated fault.
[OSB-386420 - A web service security error ocurred while producing security header]
[RouteNode_XXXXXXXXXXXXV_1_0request-pipeline]
[instanceid - *********************************************;]
</faultstring>
</soap-env:Fault>
</soap-env:Body>
</soap-env:Body>
</responseSelectedForConsumingProxyService>
作为评论的结果,我还尝试了以下选项,结果完全相同:
request.Headers["Authorization"] = "Basic " + System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("UTF-8").GetBytes("<ID>:<Pssword>"));
String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes("<ID>:<Pssword>"));
request.Headers.Add("Authorization", "Basic " + encoded);
String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("UTF-8").GetBytes("<ID>:<Pssword>"));
request.Headers.Add("Authorization", "Basic " + encoded);
我想出了以下有效的代码:
public List<HistoryRequest> GetHistoryRequestList()
{
Configuration roamingConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
string OSBBaseUri = ConfigurationManager.AppSettings["OSBUrl"];
string OSBGetEndPoint = ConfigurationManager.AppSettings["OSBEndPoint"];
string MSDCallerId = ConfigurationManager.AppSettings["AppId"];
string Encoding = "iso-8859-1";
string Osbpw = HistoryCrypto.Decrypt(ConfigurationManager.AppSettings["CipherPass"], ConfigurationManager.AppSettings["CryptoKey"]);
CredentialCache credentialCache = new CredentialCache
{
{
new Uri(OSBBaseUri),
"NTLM",
new NetworkCredential()
{
UserName = "<UserName>",
Password = Osbpw,
Domain = ConfigurationManager.AppSettings["Domain"]
}
}
};
HttpMessageHandler handler = new HttpClientHandler()
{
Credentials = credentialCache
};
var _httpClient = new HttpClient(handler)
{
BaseAddress = new Uri(OSBBaseUri),
Timeout = new TimeSpan(0, 2, 0),
};
_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("HTTP_Referrer/HistoryRequestProcess"));
_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("MSCRMCallerID/"+ MSDCallerId));
HttpContent httpContent = new StringContent("{\"operationssubtypecode\":\"HREQ\"} ", System.Text.Encoding.UTF8, "application/json");
var method = new HttpMethod("GET");
var message = new HttpRequestMessage(method, OSBGetEndPoint)
{
Content = httpContent
};
message.Headers.Add("HTTP_Referrer", "HistoryRequestProcess");
message.Headers.Add("MSCRMCallerID", MSDCallerId);
HttpResponseMessage response = _httpClient.GetAsync(OSBGetEndPoint).Result;
string content = string.Empty;
using (StreamReader stream = new StreamReader(response.Content.ReadAsStreamAsync().Result, System.Text.Encoding.GetEncoding(Encoding)))
{
content = stream.ReadToEnd();
}
return LoadHistoryRequest(content);
}