2017-05-01 13 views
0

API를 사용하여 Adobe의 Datawarehouse보고에 액세스하려고합니다. 작동 방식은 먼저 사용 가능한 메트릭과 원하는 크기로 JSON을 작성하는 것입니다. 그건 모두 잘 작동합니다. 그러나 내가해야 할 일은이 문제에 관한 것입니다. 3 ~ 4 차원 이상을 요청할 때마다 보고서를 생성하는 데 더 오래 걸립니다. HttpWebResponse이 완료 될 때까지 기다렸다가 다음 줄로 이동해야합니다. 다음은 github의 예제를 기반으로 작성한 샘플 코드입니다.C# HTTP 400 수신 대신 응답을받을 때까지 HttpWebResponse를 기다리십시오. 오류

class Program 
{ 

    protected static string JSONFolder = System.Configuration.ConfigurationManager.AppSettings["JSONFolder"]; 
    protected static string USERNAME = System.Configuration.ConfigurationManager.AppSettings["Username"]; 
    protected static string SECRET = System.Configuration.ConfigurationManager.AppSettings["Secret"]; 
    protected static string ReportSuiteID = System.Configuration.ConfigurationManager.AppSettings["ReportSuiteID"]; 
    private static string ENDPOINT = "https://api5.omniture.com/admin/1.4/rest/"; 
    protected static string environment = "dev"; 

    static void Main(string[] args) 
    { 
     DateTime previousDay = DateTime.Today.AddDays(-1); 
     string json = RequestJsonBuilder(previousDay, previousDay, ReportSuiteID); 
     string response = callMethod("Report.Queue", json, 15); 

     var jss = new JavaScriptSerializer(); 
     var requestDetails = jss.Deserialize<Dictionary<string, string>>(response); 
    } 

    /*Build the json for the methods Report.GetStatus and Report.Get*/ 
    public static string RequestJsonBuilderStatus(string id) 
    { 
     ReportID json = new ReportID() { reportID = id }; 

     var serializer = new JavaScriptSerializer(); 
     var serializedResult = serializer.Serialize(json); 
     return serializedResult; 
    } 

    /*Build the json for the method Report.Queue*/ 
    static string RequestJsonBuilder(DateTime StartDate, DateTime EndDate, string ReportSuiteID) 
    { 
     //Build the list of metrics to send with the request 
     var listMetrics = new List<Metrics>(); 
     listMetrics.Add(new Metrics() { id = "visits" }); 


     //Build the list of elements to send with the request 
     var listElements = new List<Elements>(); 
     listElements.Add(new Elements() { id = "page" , top = "25"}); 

     var serializer2 = new JavaScriptSerializer(); 
     Dictionary<string, RankedRequest> dic = new Dictionary<string, RankedRequest>(); 
     dic.Add("reportDescription", new RankedRequest() 
     { 
      reportSuiteID = ReportSuiteID, 
      dateFrom = StartDate.ToString("yyyy-MM-dd"), 
      dateTo = EndDate.ToString("yyyy-MM-dd"), 
      metrics = listMetrics, 
      elements = listElements, 
      source = "warehouse" 

     }); 
     var serializedResult2 = serializer2.Serialize(dic); 
     return serializedResult2; 
    } 

    /*Build the rest call to the Adobe Analytics REST APII 1.4*/ 
    public static String callMethod(String method, String data, int secs) 
    { 
     Program prog = new Program(); 
     HttpWebResponse statusResponse = null; 
     string responseXml = ""; 
     StringBuilder sbUrl = new StringBuilder(ENDPOINT + "?method=" + method); 
     HttpWebRequest omniRequest = (HttpWebRequest)WebRequest.Create(sbUrl.ToString()); 
     string timecreated = generateTimestamp(); 
     string nonce = generateNonce(); 
     string digest = getBase64Digest(nonce + timecreated + SECRET); 
     nonce = base64Encode(nonce); 
     omniRequest.Headers.Add("X-WSSE: UsernameToken Username=\"" + USERNAME + "\", PasswordDigest=\"" + digest + "\", Nonce=\"" + nonce + "\", Created=\"" + timecreated + "\""); 
     omniRequest.Method = "POST"; 
     omniRequest.ContentType = "text/json"; 

     //Write the json details to the request 
     using (var streamWriter = new StreamWriter(omniRequest.GetRequestStream())) 
     { 
      string json = data; 
      Console.WriteLine("\n 2.0 ############## Json request : \n\n " + json + "\n\n"); 
      streamWriter.Write(json); 
     } 

     //Get the response of the request 
     try 
     { 
      Console.WriteLine("\n 2.0 ############## Sleeping thread for " + secs + "\n"); 

      using (HttpWebResponse statusResponse = (HttpWebResponse) omniRequest.GetResponse()) 
      { 
       using (Stream receiveStream = statusResponse.GetResponseStream()) 
       { 
        using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8)) 
        { 
         responseXml = readStream.ReadToEnd(); 
         return responseXml; 
        } 
       } 
      } 
     } 
     catch (Exception ex) { throw ex; } 
    } 

    // other methods defined below 
    // private static string getBase64Digest(string input) 
    // private static string generateNonce() 
    // public static string base64Encode(string data) 
    // etc. 
} 

어떻게이 HTTP 200 응답이 실제로 수신 될 때까지 기다려야 HttpWebResponse 클래스를 어떻게해야합니까. 내가 추가하는 측정 항목 및 측정 항목의 수에 따라 때로는 1 시간에서 1 시간 정도 걸릴 수 있습니다.

how to wait until a web request with HttpWebRequest is finished?

How to process WebResponse when .NET throws WebException ((400) Bad Request)?

이 진심이 여기에 도움에 감사 : 나는 또한 시도

상황이 이런 식으로 라인 (88)을 변경 포함되어 있습니다.

감사합니다.

+1

(400)은 완전 반응입니다 ... 당신이 –

+0

400 오류 응답입니다 위해 ... 대기 할 것을 혼동된다. 나는 HTTP 200 응답을 찾기 위해 그것을 다시 말하면 어떨까요? – johnanish

답변

0

시간 제한을 설정해 보셨습니까? 제가 비슷한 질문에 대해 최근에보고 한 것으로 밝혀졌습니다 :

HttpWebRequest.GetResponse() keeps getting timed out

+0

이 그것을 시도했지만, 여전히 나에게 똑같은 것을주고있었습니다. 다시 시도하기 전에 "잠"수있는 시간을 손 앞에 알아야합니다. – johnanish