我正在尝试使用 HttpWebRequest 对象将 ASP.Net 应用程序的 web url 连接到 java web 应用程序并获得状态 200 OK。但是当我尝试使用 GetResponseStream() 读取响应内容时,我收到错误“responseStream.Length”抛出了“System.NotSupportedException”类型的异常
这是代码
string uri="https://myapp.com/mainservlet/";
System.Net.HttpWebRequest hwrequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uri);
hwrequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
hwrequest.AllowAutoRedirect = true;
hwrequest.UserAgent = "http_requester/0.1";
hwrequest.Timeout = 60000;
hwrequest.Method = "POST";
if (hwrequest.Method == "POST")
{
hwrequest.ContentType = "text/xml";
// Use UTF8Encoding instead of ASCIIEncoding for XML requests:
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
byte[] postByteArray = encoding.GetBytes(postData);
hwrequest.ContentLength = postByteArray.Length;
System.IO.Stream poststream = hwrequest.GetRequestStream();
poststream.Write(postByteArray, 0, postByteArray.Length);
poststream.Close();
}
//Get the Response now.
hwresponse = (System.Net.HttpWebResponse)hwrequest.GetResponse();
// System.Xml.Linq.XDocument doc;
//hwresponse.Method = "GET";
string str = hwresponse.CharacterSet;
string str1 = hwresponse.ContentEncoding;
if (hwresponse.StatusCode == System.Net.HttpStatusCode.OK)
{
System.IO.Stream responseStream = hwresponse.GetResponseStream();
// Here i am getting that exception
注意:当我尝试在浏览器中粘贴相同的网址时,它会说
错误 404--未从 RFC 2068 超文本传输协议中找到--HTTP/1.1: 10.4.5 404 Not Found 服务器未找到与请求 URI 匹配的任何内容。没有说明这种情况是暂时的还是永久性的。如果服务器不希望向客户端提供此信息,则可以使用状态代码 403(禁止)来代替。如果服务器通过一些内部可配置的机制知道旧资源永久不可用并且没有转发地址,则应该使用 410 (Gone) 状态代码。
它也进入 200 OK 状态并显示异常。有人可以帮我解决/建议吗?