我一直在研究一个使用 RTC API 和表单身份验证的项目。我遇到了一些奇怪的行为,但我无法弄清楚这一点。
迄今为止已经出现的场景是我可以在本地端对端成功地运行这个项目。也就是说,这段特定的代码可以:
- 联系远程服务器并成功认证
- 身份验证后,我可以通过 XML 更新 RTC 中的票证
当我发布到我们的 IIS (7.5) 服务器时,问题就开始了。一切正常,直到最后一次 .GetResponse 调用使用 PUT 方法传递我的 XML 以更新 RTC 中的票证。我不断收到“操作已超时”。
我花了几天的时间试图弄清楚这个做各种各样的事情,但没有任何东西被证明是有用的。
作为测试,我将第二次调用时的 PUT 方法更改为 GET。它有效!如果我使用带有 .AllowAutoRedirect = false 的 PUT,它的工作原理是我得到了响应,但是 RTC 端没有任何反应,因此请求显然被忽略了。我还注意到返回的状态被标记为“找到”而不是“确定”。
一些人在这个阶段认为可能是远程服务器和 Web 服务器之间缺乏连接。由于身份验证有效,并且这发生在同一台服务器上,因此情况并非如此。我还使用 Web 服务器上的 RESTClient 手动传递了 XML / PUT 调用,这很好接受。
我只是不明白为什么它在本地运行时端到端工作,但一旦部署到 IIS 就会发挥作用?
我尝试使用日志跟踪,但我不完全确定我是否从中得到任何有用的东西。它可能完全不相关,但我可以在 IIS 服务器上生成的日志中看到这一点:
<EventData>
<Data Name="ContextId">{00000000-0000-0000-12AF-0080000000F8}</Data>
<Data Name="ModuleName">ManagedPipelineHandler</Data>
<Data Name="Notification">128</Data>
<Data Name="HttpStatus">500</Data>
<Data Name="HttpReason">Internal Server Error</Data>
<Data Name="HttpSubStatus">0</Data>
<Data Name="ErrorCode">0</Data>
<Data Name="ConfigExceptionInfo"></Data>
</EventData>
正如我所说,我不确定这是否与我遇到的问题有关,但我认为我会分享而不是忽略它。
形成调用的代码(请原谅编码标准,它正在进行中,尝试不同的东西来解决这个问题变得很混乱)
//Setup webrequest
CookieContainer _cookies = new CookieContainer();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(getPath);
var test44 = test4.ToString();
request.CookieContainer = _cookies;
request.ContentType = "application/rdf+xml";
request.Accept = "application/rdf+xml";
request.Method = "PUT";
request.AllowAutoRedirect = true;
request.AllowWriteStreamBuffering = true;
request.Timeout = 40000;
byte[] bytes = Encoding.ASCII.GetBytes(test44);
request.ContentLength = bytes.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(bytes, 0, bytes.Length);
dataStream.Close();
//Pass request
logger.Info("Made it up to start of RTC request for secure document.");
using (HttpWebResponse getrespn = requestSecureDocument(request, "https://myserver:9100/jazz", "username", "pass", test44))
{
//Stream ReceiveStream = getrespn.GetResponseStream();
// Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
//StreamReader readStream = new StreamReader(ReceiveStream);
//response = readStream.ReadToEnd();
getrespn.Close();
}
与 RTC 服务器交互的代码段(基于以下示例:https ://nkumar83.wordpress.com/2013/06/13/sumption-rtc-rational-team-concert-oslc-apis-using-c -post-1-authentication/用我自己的调整):
public static HttpWebResponse requestSecureDocument(HttpWebRequest _requestItem, string _rtcServerURL, string _userName, string _password, string passXml)
{
try
{
//FormBasedAuth Step 1: Request the resource
HttpWebRequest _request = (HttpWebRequest)WebRequest.Create(_requestItem.RequestUri);
_request.CookieContainer = _requestItem.CookieContainer;
//store the response in _docResponse variable
HttpWebResponse _docResponse = (HttpWebResponse)_request.GetResponse();
//HttpStatusCode.OK indicates that the request succeeded
if (_docResponse.StatusCode == HttpStatusCode.OK)
{
//X-com-ibm-team... header signifies form based authentication is being used
string _rtcAuthHeader = _docResponse.Headers["X-com-ibm-team-repository-web-auth-msg"];
if ((_rtcAuthHeader != null) && _rtcAuthHeader.Equals("authrequired"))
{
_docResponse.GetResponseStream().Flush();
_docResponse.Close();
//Prepare form for authentication
HttpWebRequest _formPost = (HttpWebRequest)WebRequest.Create(_rtcServerURL + "/j_security_check");
_formPost.Method = "POST";
_formPost.Timeout = 30000;
_formPost.CookieContainer = _request.CookieContainer;
_formPost.Accept = "text/xml";
_formPost.ContentType = "application/x-www-form-urlencoded";
string _authString = "j_username=" + _userName + "&j_password=" + _password;
Byte[] _outBuffer = Encoding.UTF8.GetBytes(_authString);
_formPost.ContentLength = _outBuffer.Length;
Stream _str = _formPost.GetRequestStream();
_str.Write(_outBuffer, 0, _outBuffer.Length);
_str.Close();
//FormBasedAuth Step 2: Submit the login form and get response
HttpWebResponse _formResponse = (HttpWebResponse)_formPost.GetResponse();
_rtcAuthHeader = _formResponse.Headers["X-com.ibm-team.repository-web-auth-msg"];
//Check if auth failed
if ((_rtcAuthHeader != null) && _rtcAuthHeader.Equals("authfailed"))
{
//auth fialed
var fail = "";
}
else
{
//login successful
//FormBasedAuth Step 3: Resend the request for the protected resource
_formResponse.GetResponseStream().Flush();
_formResponse.Close();
using (HttpWebResponse getresp = (HttpWebResponse)_requestItem.GetResponse()) *** THIS IS TH LINE WHICH THROWS THE EXCEPTION ***
{
return getresp;
}
}
}
}
return _docResponse;
}
catch (WebException e)
{
var filePath = AppDomain.CurrentDomain.GetData("DataDirectory") + @"/trapA.xml";
using (StreamWriter writer = new StreamWriter(filePath, true))
{
writer.WriteLine("Message: Failed to trigger getresponse successfully: " + e);
}
}
return null;
}
希望有人可以提供帮助:o)