本文介绍了如何维护httpwebrequest的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨我已经使用以下代码进行了身份验证,并且现在使用此身份验证成功实现了它,我还必须继续从url"http://localhost:8080/geonetwork/srv/en/xml.metadata.get"获取响应这是我用来获取身份验证的代码
受保护的 无效 Page_Load(对象发件人,EventArgs e) { 字符串 RequestXML = " < request>" + " <用户名> admin</用户名>" + " <密码> admin</密码></request>" ; 字符串 ServerURL = " http://localhost:8080/geonetwork/srv/en/xml.user.login"; 字符串 ResponseXML = postRequest(RequestXML,ServerURL); Label1.Text = ResponseXML; } 私有 字符串 postRequest(字符串 RequestXML,字符串 ServerURL) { int 超时= 90000 ; int connectionLimit = 10 ; 字符串 responseXML = 字符串 .Empty; 尝试 { HttpWebRequest webRequest =(HttpWebRequest)WebRequest.Create(ServerURL); webRequest.Timeout =超时; webRequest.KeepAlive = true ; webRequest.ServicePoint.ConnectionLimit = connectionLimit; webRequest.Method = " POST" ; webRequest.ContentType = " 文本/xml" ; 字节 [] byteArray = Encoding.UTF8.GetBytes(RequestXML); 流strm = webRequest.GetRequestStream(); strm.Write(byteArray, 0 ,byteArray.Length); strm.Close(); HttpWebResponse webResponse =(HttpWebResponse)webRequest.GetResponse(); 编码enc = Encoding.GetEncoding(" utf-8" ); StreamReader reader = 新 StreamReader(webResponse.GetResponseStream(),enc); responseXML = reader.ReadToEnd(); reader.Close(); webResponse.Close(); } 捕获(例外) { 扔(例如); } 返回 responseXML; }
使用上面的代码,我必须从该URL"http://localhost:8080/geonetwork/srv/en/xml.metadata.get"中获取响应,而不会丢失身份验证,请pl帮助我做到这一点
.
解决方案
hii have used the following code to get authenticated and i successfully achieved it now using this authentication i have to futher proceed to get response from the url"http://localhost:8080/geonetwork/srv/en/xml.metadata.get"
this is the code i have used to get authentication
protected void Page_Load(object sender, EventArgs e) { string RequestXML = "<request>" + "<username>admin</username>" + "<password>admin</password></request>"; string ServerURL = "http://localhost:8080/geonetwork/srv/en/xml.user.login"; string ResponseXML = postRequest(RequestXML, ServerURL); Label1.Text = ResponseXML; } private string postRequest(String RequestXML, string ServerURL) { int timeout = 90000; int connectionLimit = 10; string responseXML = string.Empty; try { HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(ServerURL); webRequest.Timeout = timeout; webRequest.KeepAlive = true; webRequest.ServicePoint.ConnectionLimit = connectionLimit; webRequest.Method = "POST"; webRequest.ContentType = "text/xml"; byte[] byteArray = Encoding.UTF8.GetBytes(RequestXML); Stream strm = webRequest.GetRequestStream(); strm.Write(byteArray, 0, byteArray.Length); strm.Close(); HttpWebResponse webResponse =(HttpWebResponse)webRequest.GetResponse(); Encoding enc = Encoding.GetEncoding("utf-8"); StreamReader reader = new StreamReader(webResponse.GetResponseStream(),enc); responseXML = reader.ReadToEnd(); reader.Close(); webResponse.Close(); } catch (Exception ex) { throw (ex); } return responseXML; }
using the above code i have to get the response from this url "http://localhost:8080/geonetwork/srv/en/xml.metadata.get" without losing the authentication pl help me in doing this
.
解决方案
这篇关于如何维护httpwebrequest的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!