RESTFul XML服务发送(401)未授权错误

时间:2016-01-12 18:06:41

标签: c# .net xml rest cookiecontainer

我一直在尝试使用RESTful XML服务,该服务使用名称或姓氏在搜索中返回额外数据。我想要进行身份验证的方式是使用单独的REST API请求,该请求允许我访问其余服务。但是当我调用第二个请求时它会向我发送以下错误。 "远程服务器返回错误:(401)未经授权。"。我使用的用户和密码是正确的,因此我有点卡住了为什么要发送此邮件。

using System.Net; 
using System.IO; 
using System.Xml; 
using System.Xml.XPath;

private void btnSearch_Click(object sender, EventArgs e) {
    string sURL, aURL;
    sURL = "http://url.com/data/qws/access?var1=user&var2=password";
    aURL = "http://url.mx/data/qws/pepsp?name=miguel&lastname=osorio";

    CookieContainer cookieQ = new CookieContainer(); 
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(sURL);
    request.CookieContainer = cookieQ;

    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    request = (HttpWebRequest)HttpWebRequest.Create(aURL);
    request.CookieContainer = cookieQ;
    response = (HttpWebResponse)request.GetResponse();

    using (response) {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(response.GetResponseStream());
        XmlNode name = xmlDoc.SelectSingleNode("/xml/person/name");
        XmlNode lastname = xmlDoc.SelectSingleNode("/xml/person/lastname");
        MessageBox.Show(name.InnerText + ' ' + lastname.InnerText);
    }
}

0 个答案:

没有答案