我需要从C#代码中调用两个sap的odata URL。 第一个URL将设置过滤器,第二个URL(通用URL)将给出实际的响应。
第一个URL:
https:// <> / sap / opu / odata / SALM / TM_SERVICE / setFilters?sap-language = EN&filter ='2%2Ctime%3D%E2%80%8E4%E2%80%8E%2F%E2 %80%8E1%E2%80%8E%2F%E2%80%8E2019%E2%80%8E%20%E2%80%8E10%E2%80%8E%3A%E2%80%8E31%E2%80 %8E%3A%E2%80%8E24%E2%80%8E%20%E2%80%8EAMc%3ATest%20Suite%20for%20Focused%20Build%2Cw%3A%2CT%3D <>'
第二个URL:
https:// <> // sap / opu / odata / SALM / TM_SERVICE / TILECollection(ConfigID ='1%20',PageID ='2%20',TileID ='8%20')?$ format = json&sap-language = EN
第二个URL是通用URL和常量URL,但是在第一次执行具有不同参数值的第一个URL之后,第二个URL将给出不同的响应。
在浏览器中,我能够执行这两个URL,并查看具有不同参数值的响应差异。
需要C#代码来模拟浏览器行为。在C#代码中,第二个URL响应是恒定的,不会被更改。
尝试使用以下命令:WebRequest,HttpWebRequest,HttpClient,WebClient以及传递的cookie(执行第二个url时将从第一个响应接收的cookie传递给Request)
var httpClientHandler = new HttpClientHandler()
{
Credentials = credential,
CookieContainer = cookieContainer
};
using (HttpClient client = new HttpClient(httpClientHandler))
{
using (HttpResponseMessage response = client.GetAsync("<<first url>>").Result)
{
Uri uri = new Uri(string.Format(ConfigurationManager.AppSettings["TestPlanSetFilters"], s));
IEnumerable<Cookie> responseCookies = cookieContainer.GetCookies(uri).Cast<Cookie>();
foreach (Cookie cookie in responseCookies)
{
cookieContainer.Add(cookie);
}
}
using (HttpResponseMessage response1 = client.GetAsync("<<second url>>").Result)
{
using (HttpContent content1 = response1.Content)
{
var json2 = content1.ReadAsStringAsync().Result;
}
}
}