簡體   English   中英

HttpWebResponse.GetResponseStream()。Length引發異常,即使結果為200 OK狀態

[英]HttpWebResponse.GetResponseStream().Length throws exception even result is 200 OK status

我試圖使用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未找到服務器找不到與請求URI匹配的任何內容。 沒有跡象表明這種情況是暫時的還是永久的。 如果服務器不希望將此信息提供給客戶端,則可以改用狀態代碼403(禁止)。 如果服務器通過某種內部可配置的機制得知舊資源永久不可用並且沒有轉發地址,則應使用410(已消失)狀態代碼。

它還進入200 OK狀態並顯示異常。 有人可以幫我解決/建議嗎?

用它來獲取已經使用了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();

為什么要檢查它是否是帖子?

嘗試像這樣讀取流。 然后,您還可以設置所需的網頁數據量。

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

您必須添加自己的錯誤處理...。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM