如何维护httpwebrequest的状态 [英] how to maintain the state of the httpwebrequest
本文介绍了如何维护httpwebrequest的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我用来获取身份验证的代码
受保护的 无效 Page_Load(对象发件人,EventArgs e)
{
字符串 RequestXML = " +
" +
" ;
字符串 ServerURL = " ;
字符串 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 = " ;
webRequest.ContentType = " ;
字节 [] byteArray = Encoding.UTF8.GetBytes(RequestXML);
流strm = webRequest.GetRequestStream();
strm.Write(byteArray, 0 ,byteArray.Length);
strm.Close();
HttpWebResponse webResponse =(HttpWebResponse)webRequest.GetResponse();
编码enc = Encoding.GetEncoding(" );
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帮助我做到这一点
.
解决方案
如 RaisKazi [ ^ ]已经写过在对您的问题的评论中,HTTP本身是无状态的.您想要的任何状态都必须由服务器应用程序实现.客户端可以传递一个cooky或在先前的响应中传递回该cooky的东西,以向服务器标识自己.为了进行身份验证,我建议您使用NetworkCredentials并让该服务侦听HTTPS上的请求.
干杯!
—MRB
hi
i 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
.
解决方案
As RaisKazi[^] already wrote in a comment to your question, HTTP is per se stateless. Any state you would want would have to be implemented by your server application. A client could pass along a cooky or something that was passed back to it in a previous response to identify themselves to the server. For authentication purposes I''d advise you to use NetworkCredentials and have the service listen to requests over HTTPS.
Cheers!
—MRB
这篇关于如何维护httpwebrequest的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文