IBM的RTC REST的API給shell腳本的例子與服務器進行身份驗證:C# - WebRequest的HTTP POST與曲奇(端口從捲曲腳本)
COOKIES=./cookies.txt
USER=my_user
PASSWORD=my_password
HOST="https://myJazzServer:9092/jazz"
curl -k -c $COOKIES "$HOST/authenticated/identity"
curl -k -L -b $COOKIES -c $COOKIES -d j_username=$USER -d j_password=$PASSWORD "$HOST/authenticated/j_security_check"
這完美的作品,但我需要驗證與服務器使用C#。
到目前爲止,我有以下的,但它不工作(返回授權失敗頁):
CookieContainer _cookie;
public string _RTC()
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://myJazzServer.com:9092/jazz/authenticated/identity");
if (_cookie == null)
{
_cookie = new CookieContainer();
}
string a;
request.CookieContainer = _cookie;
using (var response = request.GetResponse())
{
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
a = sr.ReadToEnd();
}
}
byte[] data = (new ASCIIEncoding()).GetBytes("j_username=myUser&j_password=MyPass");
request = (HttpWebRequest)WebRequest.Create("https://myJazzServer.com:9092/jazz/authenticated/j_security_check");
request.Method = "POST";
request.ContentType = "text/html";
request.ContentLength = data.Length;
request.CookieContainer = _cookie;
Stream reqStream = request.GetRequestStream();
reqStream.Write(data,0,data.Length);
string b;
using (var response = request.GetResponse())
{
using (var reader = new StreamReader(response.GetResponseStream()))
{
b = reader.ReadToEnd();
}
}
}
你能更具體一點關於什麼是「不工作」?您是否收到異常,響應中沒有內容,響應中有意外的http狀態代碼(例如404,407等)? – Nathan 2010-07-17 17:17:50
@Nathan,它返回一個授權失敗的頁面,我不能給出詳細信息,因爲它返回的是一個名爲authFailed(或類似的東西)的方法的ajax引導程序,所以我無法得到實際的細節。 – 2010-07-17 17:26:09