2013-03-14 3 views
1

내 방법은 다음과 같습니다WebRequest 클래스 매우 느린

public string Request(string action, NameValueCollection parameters, uint? timeoutInSeconds = null) 
{ 
    parameters = parameters ?? new NameValueCollection(); 
    ProvideCredentialsFor(ref parameters); 

    var data = parameters.ToUrlParams(); // my extension method converts the collection to a string, works well 

    byte[] dataStream = Encoding.UTF8.GetBytes(data); 
    string request = ServiceUrl + action; 
    var webRequest = (HttpWebRequest)WebRequest.Create(request); 
    webRequest.AllowAutoRedirect = false; 
    webRequest.Method = "POST"; 
    webRequest.ContentType = "application/x-www-form-urlencoded"; 
    webRequest.ContentLength = dataStream.Length; 
    webRequest.Timeout = (int)(timeoutInSeconds == null ? DefaultTimeoutMs : timeoutInSeconds * 1000); 
    webRequest.Proxy = null; // should make it faster... 

    using (var newStream = webRequest.GetRequestStream()) 
    { 
     newStream.Write(dataStream, 0, dataStream.Length); 
    } 
    var webResponse = (HttpWebResponse)webRequest.GetResponse(); 

    string uri = webResponse.Headers["Location"]; 

    string result; 
    using (var sr = new StreamReader(webResponse.GetResponseStream())) 
    { 
     result = sr.ReadToEnd(); 
    } 


    return result; 
} 

서버는 응답 JSON을 보냅니다. 작은 JSON에서는 잘 작동하지만 큰 것을 요청할 때 문제가 발생합니다. 대형 광고는 브라우저에 표시되는 데 1 ~ 2 분이 소요되는 것을 의미합니다 (Google 크롬, 서버 측 생성 시간 포함). 사실 412KB의 텍스트입니다. 위의 메서드를 사용하여 동일한 JSON을 요청하려고하면 웹 예외 (시간 초과)가 발생합니다. 타임 아웃을 10 분으로 변경했습니다 (크롬보다 적어도 5 배 이상). 여전히 동일합니다.

아이디어가 있으십니까?

편집

이 MS 기술을 함께 할 수있는 뭔가를 갖고있는 것 같아요. IE에서는이 JSON도로드되지 않습니다.

답변

0

요청을 완료했는지 확인하십시오. 그렇지 않으면 허용 된 최대 연결 수 (한 번에 나에게 4로 낮게)를 맞추면 이전 연결이 만료 될 때까지 기다려야합니다. 사용하는 것이 가장 좋습니다

using (var response = webRequest.GetResponse()) {...