C# HttpWebResponse.GetResponseStream().Length抛出异常,即使结果为200 OK状态
我正在尝试使用HttpWebRequest对象将web url从ASP.Net应用程序连接到java web应用程序,并获得状态200 OK。但是,当我尝试使用GetResponseStream()读取响应内容时,我得到一个错误“responseStream.Length”引发了一个类型为“System.NotSupportedException”的异常 这是密码C# HttpWebResponse.GetResponseStream().Length抛出异常,即使结果为200 OK状态,c#,asp.net,httpwebrequest,httpwebresponse,C#,Asp.net,Httpwebrequest,Httpwebresponse,我正在尝试使用HttpWebRequest对象将web url从ASP.Net应用程序连接到java web应用程序,并获得状态200 OK。但是,当我尝试使用GetResponseStream()读取响应内容时,我得到一个错误“responseStream.Length”引发了一个类型为“System.NotSupportedException”的异常 这是密码 string uri="https://myapp.com/mainservlet/"; System.Net.HttpWebRe
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
注意:当我尝试在浏览器中粘贴相同的url时,它会显示
错误404--找不到
来自RFC 2068超文本传输协议——HTTP/1.1:
10.4.5 404未找到
服务器未找到任何与请求URI匹配的内容。没有说明该情况是暂时的还是永久的。
如果服务器不希望将此信息提供给客户端,则可以使用状态代码403(禁止)。如果服务器通过一些内部可配置的机制知道旧资源永久不可用且没有转发地址,则应使用410(Gone)状态代码
它也进入了200 OK状态并显示异常。有人能帮我解决/建议一下吗 你为什么要检查它是否是帖子 尝试像这样读取流。然后,您还可以设置您实际需要的网页数据量
Dim ResponseStream As Stream = Response.GetResponseStream()
Try
Dim reader As New StreamReader(Stream.Null)
Try
reader = New StreamReader(ResponseStream, System.Text.Encoding.GetEncoding(CharSet), True, 256)
Catch ex As Exception
'
' Redo the stream
'
If InStr(ex.Message, "not a supported encoding name.") > 0 Then
Try
reader = New StreamReader(ResponseStream, System.Text.Encoding.GetEncoding("utf-8"), True, 256)
Catch ex1 As Exception
End Try
End If
End Try
'
'Dim string to build index.
'
Dim IndexBuildString As New StringBuilder()
Try
Dim read(256) As [Char]
'
' Reads 256 characters at a time.
'
Dim ByteCount As Integer = reader.Read(read, 0, 256)
'
' Read in while, exit it if exceeds ~ 390 kb
'
While ByteCount > 0
'
'Check if limit of kilobytes are over the limit.
'
If System.Text.ASCIIEncoding.Unicode.GetByteCount(IndexBuildString.ToString()) > 153600 Then
Exit While
End If
'
'Dumps the 256 characters to a string and displays the string to the console.
'
Dim str As New [String](read, 0, ByteCount)
ByteCount = reader.Read(read, 0, 256)
'
'Append to the StringBuilder
'
IndexBuildString.Append(str)
End While
'
'Assign the Stringbuilder and then clear it
'
IndexString = CleanIndexString(IndexBuildString.ToString())
Catch ex As Exception
Finally
Try
IndexBuildString.Clear()
reader.Close()
reader.Dispose()
Catch ex As Exception
End Try
End Try
Catch ex As Exception
Finally
ResponseStream.Dispose()
End Try
您必须添加自己的错误处理…为什么要检查它是否是帖子 尝试像这样读取流。然后,您还可以设置您实际需要的网页数据量
Dim ResponseStream As Stream = Response.GetResponseStream()
Try
Dim reader As New StreamReader(Stream.Null)
Try
reader = New StreamReader(ResponseStream, System.Text.Encoding.GetEncoding(CharSet), True, 256)
Catch ex As Exception
'
' Redo the stream
'
If InStr(ex.Message, "not a supported encoding name.") > 0 Then
Try
reader = New StreamReader(ResponseStream, System.Text.Encoding.GetEncoding("utf-8"), True, 256)
Catch ex1 As Exception
End Try
End If
End Try
'
'Dim string to build index.
'
Dim IndexBuildString As New StringBuilder()
Try
Dim read(256) As [Char]
'
' Reads 256 characters at a time.
'
Dim ByteCount As Integer = reader.Read(read, 0, 256)
'
' Read in while, exit it if exceeds ~ 390 kb
'
While ByteCount > 0
'
'Check if limit of kilobytes are over the limit.
'
If System.Text.ASCIIEncoding.Unicode.GetByteCount(IndexBuildString.ToString()) > 153600 Then
Exit While
End If
'
'Dumps the 256 characters to a string and displays the string to the console.
'
Dim str As New [String](read, 0, ByteCount)
ByteCount = reader.Read(read, 0, 256)
'
'Append to the StringBuilder
'
IndexBuildString.Append(str)
End While
'
'Assign the Stringbuilder and then clear it
'
IndexString = CleanIndexString(IndexBuildString.ToString())
Catch ex As Exception
Finally
Try
IndexBuildString.Clear()
reader.Close()
reader.Dispose()
Catch ex As Exception
End Try
End Try
Catch ex As Exception
Finally
ResponseStream.Dispose()
End Try
您必须添加您自己的错误处理…使用此项获得响应。使用此项已4年,现在始终有效
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
Stream responseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(responseStream);
responseData = myStreamReader.ReadToEnd();
}
response.Close();
用这个来得到回应已经用了4年了现在总是有效的
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
Stream responseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(responseStream);
responseData = myStreamReader.ReadToEnd();
}
response.Close();
你有没有用Fiddler检查过留言和回复?如果您无法获得响应流,我会有点怀疑200状态。Net Java非常有趣。你用Fiddler检查过消息和响应了吗?如果您无法获得响应流,我会有点怀疑200状态。Net Java可能非常有趣。