C#通过http 方式获取返回的XML

.Net技术 码拜 8年前 (2016-02-23) 1977次浏览
如题,应该怎么样实现。当然,有用户名密码的情况下,应该怎么写?网上基本都是没有验证的情况下的代码。
与客户做对接,通过 https访问url,给了用户名密码 输入后 返回 XML,在IE上测试通过,IE测试的时候弹出验证框是,WINDOWS安全那个,然后输入。
怎么样实现
验证方式的原始说明如下  SSL connection
Getting Started
The 客户 XML API is implemented to handle HTTP GET or POST requests to returns XML data.
Connection & Authentication
The 客户 API is not public. In order to have access to the API, Vendor must :
Fill the online form in order to get access to the API.
Add the 客户 certificate to the ‘Trusted Root Certification Authorities’.
The version 1 of API uses the HTTP Basic authentication over a secure SSL connection.
Calls to the API are stateless. Because the XML API is restricted to specific IPs, Only approved vendor will be able to communicate with 客户.
How to invoke 客户 services
Your client must send an HTTP request to the 客户 URL endpoint.
解决方案

15

用System.Net.NetworkCredential试试

2

家里没网,不然就给你发过去了。C#通过http 方式获取返回的XML

35

无非就是验证的问题.
相似登陆路由器重启路由器差不多是一个道理.

string name = "admin";
            string pwd = "admin";
            CookieContainer cookie = new CookieContainer();
            string url = "http://192.168.1.1";
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req = (HttpWebRequest)WebRequest.Create("http://192.168.1.1/????????");
            req.Method = "POST";
            req.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; qdesk 2.5.1269.201; Windows NT 6.1; Trident/5.0)";
            req.CookieContainer = cookie;
            req.KeepAlive = true;
            req.Accept = "text/html, application/xhtml+xml, */*";
            req.PreAuthenticate = true;
            CredentialCache cache = new CredentialCache();
            cache.Add(new Uri(url), "Basic", new NetworkCredential(name, pwd));
            req.Credentials = cache;
            HttpWebResponse rs = (HttpWebResponse)req.GetResponse();
            rs.Cookies = cookie.GetCookies(req.RequestUri);
            
            var io = new System.IO.StreamReader(rs.GetResponseStream(), System.Text.Encoding.Default);
            var str = io.ReadToEnd();
            Console.WriteLine(str);
            io.Close();
            rs.Close();

9

请求的时候就包含用户名密码,然后在服务端验证了,加一个session

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明C#通过http 方式获取返回的XML
喜欢 (0)
[1034331897@qq.com]
分享 (0)