2011-02-12 3 views
0

그래서 HTTPwebrequests에 사용하는이 클래스가 있습니다. 내 프로그램의이 인스턴스에서는 쿠키뿐만 아니라 일부 데이터를 게시하고 있습니다. 이제 정말 이상한 부분을 털어 놓습니다. 필자는 헤더를 냄새 맡고 테스트 할 때 내 프로그램의 트래픽을 모니터링하기 위해 바이올린을 사용합니다. 피들러가 열려 있으면이 함수는 정상적으로 작동합니다. anydata를 게시 할 때마다 fidler가 닫히면 작업 시간이 초과됩니다. 그것은 항상 이것을합니다. 내가 무엇을 하던지. 나가 좌절에서 나의 머리를 끌어 당기는 때 누군가는 이것에 약간 빛을 비울 수 있는다.HttpWebRequest 및 응답 문제

Public Function Send(ByVal URL As String, _ 
    Optional ByVal PostData As String = "", _ 
    Optional ByVal Method As HTTPMethod = HTTPMethod.HTTP_GET, _ 
    Optional ByVal ContentType As String = "", Optional ByVal Refer As String = "") 

    Dim Request As HttpWebRequest = WebRequest.Create(URL) 
    Dim Response As HttpWebResponse 
    System.Net.ServicePointManager.Expect100Continue = False 
    Dim SW As StreamWriter 
    Dim SR As StreamReader 
    Dim ResponseData As String 
    Dim a As New CookieContainer() 
    ' Request.Proxy = New WebProxy("173.234.250.164", 3128) 
    ' Prepare Request Object 
    Request.Method = Method.ToString().Substring(5) 
    Request.CookieContainer = a 
    ' Set form/post content-type if necessary 
    If (Method = HTTPMethod.HTTP_POST AndAlso PostData <> "" AndAlso ContentType = "") Then 
     ContentType = "application/x-www-form-urlencoded" 
    End If 


    'Set User Agent 
    Request.UserAgent = ("Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; GTB6.6)") 

    'Set Refer 
    If (Refer <> "") Then 
     Request.Referer = Refer 
    End If 


    ' Set Content-Type 
    If (ContentType <> "") Then 
     Request.ContentType = ContentType 
     Request.ContentLength = PostData.Length 
    End If 

    'Set Cookie If Given 
    If (Cookie <> "") Then 
     Request.Headers.Add("cookie", Cookie) 
    End If 


    ' Send Request, If Request 
    If (Method = HTTPMethod.HTTP_POST) Then 
     Try 
      Debug.Print("Inside Post") 
      SW = New StreamWriter(Request.GetRequestStream()) 
      SW.Write(PostData) 
      Debug.Print("Wrote Post Data") 
     Catch Ex As Exception 
      Throw Ex 
     Finally 
      SW.Close() 
     End Try 
    End If 

    ' Receive Response 
    Try 
     Response = Request.GetResponse() 
     For Each cook As Cookie In Response.Cookies 
      Cookie = Cookie & cook.ToString() & ";" 
     Next 
     Debug.Print(Cookie) 

     SR = New StreamReader(Response.GetResponseStream()) 
     ResponseData = SR.ReadToEnd() 
    Catch Wex As System.Net.WebException 
     SR = New StreamReader(Wex.Response.GetResponseStream()) 
     ResponseData = SR.ReadToEnd() 
     Throw New Exception(ResponseData) 
    Finally 
     SR.Close() 
    End Try 

    Return ResponseData 


End Function 
+0

WCF에서 비슷한 문제가 발생했습니다. http://stackoverflow.com/questions/1646985/wcf-client-hang-on-service-interruption/1659667#1659667. 너처럼 보이는데. 프로그래밍 방식으로 동일한 설정을 적용하므로 솔루션처럼 보이지 않습니다. – roufamatic

답변

0

HttpWebResponse은 절대로 연결을 열지 않음을 의미합니다. Response에 대해 Using 문을 사용해야합니다. (또한 메서드의 맨 위에있는 모든 것을 선언하는 대신 가능한 한 늦게 변수를 선언하는 것이 좋습니다 ... 그리고 블록을 닫는 대신 Using 문을 사용하는 것이 좋습니다.

또한 SR을 finally 블록에서 닫을 때 설정되지 않을 수 있습니다. 예외가 발생하지 않는다고 예상되는 경우 SR을 닫으려는 시도가 NullReferenceException 일 수 있습니다. 개인적으로 두 개의 StreamReaders 케이스 하나는 실패 사례).