我已经获得了两项服务,其中 ist 服务用于登录目的,它将返回一个 xml 节点令牌,我将在所有其他服务调用中将其添加为标头,
在附加的示例代码中,函数CreateObject()中有两个函数,我正在点击登录服务并且能够成功获取响应,并且从响应中我获取了令牌并使用令牌搜索(字符串令牌)调用其他函数并将令牌传入标题,但它给出了一个例外[System.Net.WebException] = {"The remote server returned an error: (403) Forbidden."}
但是当我在 chrome Advanced Rest Client 中看到它给出了预期的响应
任何朝着正确方向的建议或解决方案都将受到高度评价
请参阅下面的代码
private void CreateObject()
{
try
{
string abc = "";
Stream datastream;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("www.example.com/abc/login");
request.CookieContainer = new CookieContainer();
request.AllowAutoRedirect = true;
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
request.ContentType = "application/x-www-form-urlencoded";
request.Accept = "application/xml";
request.KeepAlive = true;
String postData = "";
request.Method = "POST";
postData = String.Format("username={0}&password={1}", "sample", "sample");
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentLength = byteArray.Length;
datastream = request.GetRequestStream();
datastream.Write(byteArray, 0, byteArray.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
datastream = response.GetResponseStream();
String sourceCode = "";
using (StreamReader reader = new StreamReader(datastream))
{
sourceCode = reader.ReadToEnd();
}
int pos = sourceCode.IndexOf("<?");
string xmlpart = sourceCode.Substring(pos);
//XmlReader reader1 = XmlReader.Create(sourceCode);
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlpart);
XmlNodeList objNodeList = doc.GetElementsByTagName("token");
string tokens = objNodeList[0].ChildNodes.Item(0).InnerText.Trim();
search(tokens);
}
catch (Exception e)
{
}
}
public void search(string token)
{
Stream datastream;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://example.com/services/suggestions/search");
request.CookieContainer = new CookieContainer();
// request.AllowAutoRedirect = true;
System.Net.ServicePointManager.Expect100Continue = false;
request.Headers.Add("x-csrf-token", token);
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
request.ContentType = "application/x-www-form-urlencoded";
request.Accept = "application/x-cdf,text/html,application/xhtml+xml,application/json,application/xml;q=0.9,*/*;q=0.8";
String postData = "";
request.Method = "POST";
request.KeepAlive = true;
postData = String.Format("param1={0}¶m2={1}¶m3={2}¶m4={3}¶m5={4}¶m6={5}¶m7={6}¶m8={7}", "0", "1","1","1","0","1","2015-06-22","2015-06-22");
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentLength = byteArray.Length;
datastream = request.GetRequestStream();
datastream.Write(byteArray, 0, byteArray.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
datastream = response.GetResponseStream();
String sourceCode = "";
using (StreamReader reader = new StreamReader(datastream))
{
sourceCode = reader.ReadToEnd();
}
}