2012-09-20 2 views
5
코드의 아래 부분은 오류 메시지를주고있다

: 오류 Sourse "작업이 초과되었습니다"System.Net.httpWebRequest.GetResponse (에서)System.Net.WebRequest - 시간 초과 오류

이를 메서드는 URL을 호출하고 응답 객체를 가져 오는 중입니다.

참고 :이 내가 production..it에 동일한 코드를 보내 내 end..but의 모든 작업을 잘 시간 oout 오류

public GetUpdatedInventoryUnitValues(Vehicle aeVehicle) 
{ 
      WebRequest oWebRequest = null; 
      StringBuilder oStringBuilder = null; 
      StreamReader oStreamReader = null; 
      dcDealerDetails = new Dictionary<string, string>(); 

      MSRP = string.Empty; 
      NetPrice = string.Empty; 
      string strLine = string.Empty; 
      string strURL = GetUpdatedInventoryUnitValues.GetFormattedURL(aeVehicle); 

      try 
      { 
       /* Open the requested URL */ 
       oWebRequest = WebRequest.Create(strURL); 
       oWebRequest.Method = "GET"; 
       oWebRequest.ContentType = "application/xml"; 
       /* Get the stream from the returned web response */ 
       oStreamReader = new StreamReader(oWebRequest.GetResponse().GetResponseStream()); 
       /* Get the stream from the returned web response */ 
       oStringBuilder = new StringBuilder(); 
       /* Read the stream a line at a time and place each one into the stringbuilder */ 
       while ((strLine = oStreamReader.ReadLine()) != null) 
       { 
        /* Ignore blank lines */ 
        if (strLine.Length > 0) 
         oStringBuilder.Append(strLine); 
       } 

       string[] tempArray = null; 
       string[] tempNextArray = null; 
       //Split string by semicolon as a separater 
       tempArray = Data.SplitString(oStringBuilder.ToString(), new char[] { ';' }); 

       if (tempArray != null) 
       { 
        foreach (string invUnits in tempArray) 
        { 
         //Split string by '=' as a separater 
         tempNextArray = Data.SplitString(invUnits, new char[] { '=' }); 

         if (tempNextArray != null && tempNextArray.Length == 2) 
         { 
          switch (tempNextArray[0].ToLower()) 
          { 
           //case "msrp": 
           // MSRP = Data.RemoveDoubleCode(tempNextArray[1]); 
           // break; 
           case "netprice": 
            NetPrice = Data.RemoveDoubleCode(tempNextArray[1]); 
            break; 
          } 
         } 
        } 
       } 
      } 
      catch (Exception ex) 
      { 
       ErrorLog.ErrorMessage = ErrorLog.Separator; 
       ErrorLog.ErrorMessage = "Exception during posting data to another application ."; 
       ErrorLog.ErrorMessage = "ERROR MESSAGE : " + ex.Message; 
       ErrorLog.ErrorMessage = "ERROR SOURCE: " + ex.StackTrace.ToString(); 

      } 
      finally 
      { 
       if (oStreamReader != null) 
       { 
        oStreamReader.Close(); 
       } 
       if (oWebRequest != null) 
       { 
        oWebRequest = null; 
       } 
      } 
     } 

내가 잘못하고 또는 실종 무엇을 제안하십시오

을 보여줍니다?

+0

당신은 확실 요청 좋은이며 코드에서 타임 아웃하지 않습니다 ... 문제를 해결? 브라우저에서 직접 실행하려고 했습니까? –

+0

내 마지막에는 괜찮습니다.하지만 같은 코드를 생산품에 보내면 시간이 오래 걸립니다. –

+0

개발 플랫폼과 생산 플랫폼의 차이점은 무엇입니까? 귀하의 요청을 막을 수있는 방화벽 또는 이와 유사한 것이 있습니까? –

답변

17

처음 두 요청이 괜찮은 것을 알고 있습니까? 다음 시간이 초과 되었습니까? 그렇다면이 문제가 의심됩니다.

oStreamReader = new StreamReader(oWebRequest.GetResponse().GetResponseStream()); 

응답을 가져 왔지만 처리하지 않았습니다. 당신은 사용해야

using (var response = oWebRequest.GetResponse()) 
{ 
    ... 
} 

사실, 당신은 당신의 finally 블록을 제거 할 수는 전역 using 문을 사용하여 완전히합니다.

제쳐두고, 이것은 꽤 긴 방법입니다 - 77 줄! - 실제로 생성자처럼 악화, 그것은 같습니다

  • 시도가 작고, 더 쉽게 이해, 더 쉽게 테스트 할 수 조각으로 그것을 분할하는
  • 생성자에서 많은 작업을 수행하지 않도록하십시오
+0

.. 이건 제 250 회 루프에서 잘 작동하고 있습니다.하지만 프로덕션에서는 거의 모든 것이 타임 아웃입니다. 코드 부분에서 ... 제가해야 할 일은 ... –

+0

@RatanSharma : 제작에서 가져온 URL은 무엇입니까? 정말로 시간이 초과 될 가능성이 있습니까? –

+0

위의 기능은 tool..which입니다. 생산 현장에서 실행 중입니다 ... 함수는 일부 응답을 제공하는 URL을 호출합니다.이 도구는 내 시스템에서 실행할 때 잘 작동하지만 프로덕션 서버에서는 나는 같은 시간을 보내고있다. –

1

나는 personnally 내 프로그램 중 하나에이 코드를 사용하고 완벽하게 작동합니다

WebRequest webRequest = WebRequest.Create(requestUri); 
    webRequest.Credentials = new NetworkCredential(login, password); 
    WebResponse webResponse = webRequest.GetResponse(); 
    Stream response = webResponse.GetResponseStream(); 
    StreamReader reader = new StreamReader(response); 

그래서 내가 그것을 코드에서하지만 생산 플랫폼에서 제공되지 않습니다 생각합니다.

+4

응답을 처리하지 않으면 완벽하게 작동하지 않습니다. 단지 운이 좋았습니다. –

+0

사용 종료 후 게시 된 코드를 사용하여 응답을 처리합니까? –

+1

예. 'IDisposable'을 구현하는 거의 모든 것에'using' 문을 써서 정리를해야합니다. –

2

그냥 경험을 공유하십시오.

동일한 오류 "작업 시간 초과"가 발생했습니다.

WebClient와 WebRequest (Timeout도 설정)를 모두 사용해 보았지만 여전히 오류가 발생했습니다.

이유는 응답을 처리하지 않았기 때문입니다. 위에서 언급 한 바와 같이

그래서 내가 사용 :

using (var response = oWebRequest.GetResponse()) 

{ 
    ... 
} 

그리고