我正在尝试抓取一个网站以自动下载.csv格式的报告。这是我正在尝试使用c#在服务器端进行的网站的ajax调用:
$.ajax(
{
url: '/my-folder/generate-report',
data: { model: JSON.stringify(data) },
type: 'POST',
dataType: "json",
success: function (response) { ... },
error: function (...) { ... }
);
无论我做什么,都会收到错误消息“值不能为空。”
我首先必须登录该站点,该站点运行正常。 然后,我使用帖子调用了generate-report,它始终返回Value不能为null。
新的ReportModel()代码不包括在内,但是如果需要的话可以包含。通过实际的网站/浏览器生成报告时,它基本上只是将“数据”转换为当我查看Http标头时使用的确切格式。
这是我的C#代码:
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
class ReportModel
{
public string SampleProperty1 => "SampleValue";
public string[] SampleArrayProperty1 => new string[] {};
}
class Program
{
static void Main(string[] args)
{
var baseAddress = new Uri("https://example.com");
var cookieContainer = new CookieContainer();
using (var handler = new HttpClientHandler() { CookieContainer = cookieContainer })
using (var client = new HttpClient(handler) { BaseAddress = baseAddress })
{
var homePageResult = client.GetAsync("/my-folder/");
homePageResult.Result.EnsureSuccessStatusCode();
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("UserName", "..."),
new KeyValuePair<string, string>("Password", "..."),
});
var loginResult = client.PostAsync("/my-folder/Login", content).Result;
loginResult.EnsureSuccessStatusCode();
var myContent = JsonConvert.SerializeObject(new ReportModel());
var buffer = System.Text.Encoding.UTF8.GetBytes(myContent);
var byteContent = new ByteArrayContent(buffer);
byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var reportStep1Result = client.PostAsync("/my-folder/generate-report", byteContent);
reportStep1Result.Result.EnsureSuccessStatusCode();
var result = reportStep1Result.Result.Content.ReadAsStringAsync();
}
}
}
上面的结果表明报告无法生成,并且消息如下:
{
"Success":false,
"Message":"Value cannot be null.\r\nParameter name:value",
"Html":null,
"Model":null
}
由于我知道从浏览器中的Web应用程序调用ajax调用时效果很好,所以我认为发布该调用时缺少某些内容。
当我在Chrome开发者工具中检查HTTP标头时(从浏览器中调用时),我看到标头>表单数据>模型:{...报告参数的json表示形式...}。
也许我发送的json参数与在浏览器中发送的方式不同?
如果您需要更多详细信息,请告诉我,我们将很乐意提供。预先谢谢你。
2018年12月4日更新11:53-向ReportModel添加了更多详细信息,以显示我如何使用它,作为将其序列化为正确JSON的便捷方法。
更新12/4/2018 3:04 pm-这是在浏览器中执行报告时的请求详细信息:
"request": {
"method": "POST",
"url": "https://example.com/my-folder/generate-report",
"httpVersion": "HTTP/1.1",
"headers": [
{
"name": "Cookie",
"value": "_ga=...; SearchView=grid; HASH_SearchView=...; _gid=...; ASP.NET_SessionId=...; Emp_Id=...; TimeClockSessionKey=...; TimeClockEmp_Id=...; _gat_tracker1=1; _gat_tracker2=1; Expiration=12/4/2018 8:02:26 PM; SessionKey=..."
},
{
"name": "Origin",
"value": "https://example.com"
},
{
"name": "Accept-Encoding",
"value": "gzip, deflate, br"
},
{
"name": "Host",
"value": "example.com"
},
{
"name": "Accept-Language",
"value": "en-US,en;q=0.9"
},
{
"name": "User-Agent",
"value": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36"
},
{
"name": "Content-Type",
"value": "application/x-www-form-urlencoded; charset=UTF-8"
},
{
"name": "Accept",
"value": "application/json, text/javascript, */*; q=0.01"
},
{
"name": "Referer",
"value": "https://example.com/my-folder/generate-report/"
},
{
"name": "X-Requested-With",
"value": "XMLHttpRequest"
},
{
"name": "Connection",
"value": "keep-alive"
},
{
"name": "Content-Length",
"value": "5385"
}
],
"queryString": [],
"cookies": [
{
"name": "_ga",
"value": "...",
"expires": null,
"httpOnly": false,
"secure": false
},
{
"name": "SearchView",
"value": "grid",
"expires": null,
"httpOnly": false,
"secure": false
},
{
"name": "HASH_SearchView",
"value": ".",..
"expires": null,
"httpOnly": false,
"secure": false
},
{
"name": "_gid",
"value": "...",
"expires": null,
"httpOnly": false,
"secure": false
},
{
"name": "ASP.NET_SessionId",
"value": "...",
"expires": null,
"httpOnly": false,
"secure": false
},
{
"name": "Emp_Id",
"value": "...",
"expires": null,
"httpOnly": false,
"secure": false
},
{
"name": "TimeClockSessionKey",
"value": "...",
"expires": null,
"httpOnly": false,
"secure": false
},
{
"name": "TimeClockEmp_Id",
"value": "...",
"expires": null,
"httpOnly": false,
"secure": false
},
{
"name": "_gat_tracker1",
"value": "1",
"expires": null,
"httpOnly": false,
"secure": false
},
{
"name": "_gat_tracker2",
"value": "1",
"expires": null,
"httpOnly": false,
"secure": false
},
{
"name": "Expiration",
"value": "12/4/2018 8:02:26 PM",
"expires": null,
"httpOnly": false,
"secure": false
},
{
"name": "SessionKey",
"value": "...",
"expires": null,
"httpOnly": false,
"secure": false
}
],
"headersSize": 1068,
"bodySize": 0,
"postData": {
"mimeType": "application/x-www-form-urlencoded; charset=UTF-8",
"text": "model=%7B%22LoggedInEmployeeId%22%3A%22%22%2C%22SelectedCompanyIds%22%3A%5B%221%22%5D%2C%22SelectedRouteCodes%22%3A%5B%22NULL%22%2C%22R01%22%5D%2C%22SelectedTerritoryCodes%22%3A%5B%5D%2C%22SelectedZipCodes%22%3A%5B%2260137%22%2C%2260148%22%2C%2260174%22%2C%2260181%22%2C%2260187%22%2C%2260189%22%2C%2260190%22%2C%2260403%22%2C%2260404%22%2C%2260410%22%2C%2260431%22%2C%2260432%22%2C%2260433%22%2C%2260435%22%2C%2260436%22%2C%2260439%22%2C%2260440%22%2C%2260441%22%2C%2260446%22%2C%2260447%22%2C%2260451%22%2C%2260467%22%2C%2260490%22%2C%2260491%22%2C%2260502%22%2C%2260503%22%2C%2260504%22%2C%2260505%22%2C%2260506%22%2C%2260510%22%2C%2260512%22%2C%2260514%22%2C%2260515%22%2C%2260516%22%2C%2260517%22%2C%2260532%22%2C%2260538%22%2C%2260540%22%2C%2260543%22%2C%2260544%22%2C%2260545%22%2C%2260551%22%2C%2260554%22%2C%2260555%22%2C%2260559%22%2C%2260560%22%2C%2260561%22%2C%2260563%22%2C%2260564%22%2C%2260565%22%2C%2260585%22%2C%2260586%22%2C%2299999%22%5D%2C%22SelectedSourceCodes%22%3A%5B%22-1%22%2C%22105%22%2C%2279%22%2C%2285%22%2C%2274%22%2C%2248%22%2C%22112%22%2C%22119%22%2C%22125%22%2C%2293%22%2C%22130%22%2C%2269%22%2C%2263%22%2C%22127%22%2C%2266%22%2C%2265%22%2C%2256%22%2C%228%22%2C%2247%22%2C%2276%22%2C%2290%22%2C%22123%22%2C%2282%22%2C%2294%22%2C%22124%22%2C%2258%22%2C%2271%22%2C%22107%22%2C%2255%22%2C%22101%22%2C%22102%22%2C%22132%22%2C%22122%22%2C%22118%22%2C%2273%22%2C%2254%22%2C%2253%22%2C%2250%22%2C%2284%22%2C%2218%22%2C%2223%22%2C%2243%22%2C%2283%22%2C%2236%22%2C%2224%22%2C%2230%22%2C%2226%22%2C%2233%22%2C%2232%22%2C%2231%22%2C%2277%22%2C%22128%22%2C%2217%22%2C%22115%22%2C%22106%22%2C%22111%22%2C%22113%22%2C%2251%22%2C%2297%22%2C%2299%22%2C%2298%22%2C%2296%22%2C%2245%22%2C%2295%22%2C%22108%22%2C%22121%22%2C%2249%22%2C%22126%22%2C%22114%22%2C%22117%22%2C%2292%22%2C%2281%22%2C%2260%22%2C%2212%22%2C%2222%22%2C%2211%22%2C%2235%22%2C%2237%22%2C%22116%22%2C%2210%22%2C%22131%22%2C%2288%22%2C%2289%22%2C%2219%22%2C%2234%22%2C%229%22%2C%2278%22%2C%2264%22%2C%2244%22%2C%2280%22%2C%2275%22%2C%2286%22%2C%2259%22%2C%2270%22%2C%22129%22%2C%2291%22%2C%22100%22%2C%22120%22%2C%22103%22%2C%2246%22%2C%2240%22%2C%2225%22%2C%2261%22%2C%22109%22%2C%2242%22%2C%2252%22%2C%2241%22%2C%2228%22%2C%22133%22%2C%22110%22%2C%227%22%2C%2257%22%2C%2262%22%2C%2287%22%2C%22104%22%5D%2C%22SelectedBillingTypes%22%3A%5B%22R%22%2C%22M%22%2C%22A%22%2C%22D%22%2C%22C%22%5D%2C%22SelectedCustomerStatuses%22%3A%5B%228%22%2C%229%22%5D%2C%22SelectedTaxCodes%22%3A%5B%5D%2C%22CustomerType%22%3A%22Both%22%2C%22BillToOption%22%3A%22All%22%2C%22Season%22%3A%222018%22%2C%22SelectedPropertyInventory%22%3A%220%22%2C%22StartDate%22%3A%221753-01-01+00%3A00%3A00%22%2C%22EndDate%22%3A%229998-12-31+23%3A59%3A59%22%2C%22DateRangeText%22%3A%22All%22%2C%22BeginCustomerNumberRange%22%3A%220%22%2C%22EndCustomerNumberRange%22%3A%22999999999%22%2C%22BeginMapcodeRange%22%3A%22%22%2C%22EndMapcodeRange%22%3A%22ZZ-ZZ-ZZZ%22%2C%22BeginCarrierRouteRange%22%3A%22%22%2C%22EndCarrierRouteRange%22%3A%22ZZZZ%22%2C%22BeginSizeRange%22%3A%220%22%2C%22EndSizeRange%22%3A%229999999.99%22%2C%22IncludeCustomersWithoutServices%22%3A%22true%22%2C%22SelectedSortOrder%22%3A%22CustomerNumber%22%2C%22SummaryOnly%22%3A%22false%22%2C%22PreviewFlag%22%3A%22false%22%2C%22SelectedFlags%22%3A%22%7B%5C%22with%5C%22%3A%5B%5D%2C%5C%22without%5C%22%3A%5B%5D%7D%22%2C%22AdvanceOptionsDataAsString%22%3A%22%7B%5C%22CustomerPhone%5C%22%3A0%2C%5C%22CustomerEmail%5C%22%3A0%2C%5C%22CustomerCreditHold%5C%22%3A0%2C%5C%22GroupBillingMasterAccounts%5C%22%3A%5B%5D%2C%5C%22WithDocuments%5C%22%3A%5B%5D%2C%5C%22WithoutDocuments%5C%22%3A%5B%5D%2C%5C%22DocumentsStartDate%5C%22%3A%5C%2201%2F01%2F2018%5C%22%2C%5C%22DocumentsEndDate%5C%22%3A%5C%2212%2F31%2F2018%5C%22%2C%5C%22DocumentsDateName%5C%22%3A%5C%22This+year%5C%22%2C%5C%22WithLetters%5C%22%3A%5B%5D%2C%5C%22WithoutLetters%5C%22%3A%5B%5D%2C%5C%22LettersStartDate%5C%22%3A%5C%2201%2F01%2F2018%5C%22%2C%5C%22LettersEndDate%5C%22%3A%5C%2212%2F31%2F2018%5C%22%2C%5C%22LettersDateName%5C%22%3A%5C%22This+year%5C%22%2C%5C%22WithNPS%5C%22%3A%5B%5D%2C%5C%22WithoutNPS%5C%22%3A%5B%5D%2C%5C%22NPSStartDate%5C%22%3A%5C%2201%2F01%2F2018%5C%22%2C%5C%22NPSEndDate%5C%22%3A%5C%2212%2F31%2F2018%5C%22%2C%5C%22NPSDateName%5C%22%3A%5C%22This+year%5C%22%2C%5C%22DontTelemarket%5C%22%3A%5C%220%5C%22%2C%5C%22DontDirectMail%5C%22%3A%5C%220%5C%22%2C%5C%22DontEmail%5C%22%3A%5C%220%5C%22%2C%5C%22DontFollowupByEmail%5C%22%3A%5C%220%5C%22%2C%5C%22DontUpsell%5C%22%3A%5C%220%5C%22%2C%5C%22NoKnock%5C%22%3A%5C%220%5C%22%2C%5C%22CawRegisteredUser%5C%22%3A%5C%220%5C%22%2C%5C%22AutoPay%5C%22%3A%5C%220%5C%22%2C%5C%22PreferredLanguage%5C%22%3A%5C%220%5C%22%2C%5C%22EmailStatements%5C%22%3A%5C%220%5C%22%2C%5C%22EmailPrenotification%5C%22%3A%5C%220%5C%22%2C%5C%22TextPrenotification%5C%22%3A%5C%220%5C%22%2C%5C%22WithContactPreferred%5C%22%3A%5B%5D%2C%5C%22WithoutContactPreferred%5C%22%3A%5B%5D%2C%5C%22WithSourceSize%5C%22%3A%5B%5D%2C%5C%22WithoutSourceSize%5C%22%3A%5B%5D%2C%5C%22WithSubdivisions%5C%22%3A%5B%5D%2C%5C%22WithoutSubdivisions%5C%22%3A%5B%5D%7D%22%2C%22SendToCallLog%22%3A%22false%22%2C%22SendToCallLogData%22%3Anull%2C%22MainGrouping%22%3A%22Ignore%22%2C%22SubGrouping%22%3A%22Ignore%22%2C%22AllRouteCodes%22%3A%22true%22%2C%22AllSourceCodes%22%3A%22false%22%2C%22AllZipCodes%22%3A%22true%22%2C%22AllTerritoryCodes%22%3A%22true%22%2C%22AllTaxCodes%22%3A%22true%22%2C%22ExportToCSV%22%3Atrue%7D",
"params": [
{
"name": "model",
"value": "%7B%22LoggedInEmployeeId%22%3A%22%22%2C%22SelectedCompanyIds%22%3A%5B%221%22%5D%2C%22SelectedRouteCodes%22%3A%5B%22NULL%22%2C%22R01%22%5D%2C%22SelectedTerritoryCodes%22%3A%5B%5D%2C%22SelectedZipCodes%22%3A%5B%2260137%22%2C%2260148%22%2C%2260174%22%2C%2260181%22%2C%2260187%22%2C%2260189%22%2C%2260190%22%2C%2260403%22%2C%2260404%22%2C%2260410%22%2C%2260431%22%2C%2260432%22%2C%2260433%22%2C%2260435%22%2C%2260436%22%2C%2260439%22%2C%2260440%22%2C%2260441%22%2C%2260446%22%2C%2260447%22%2C%2260451%22%2C%2260467%22%2C%2260490%22%2C%2260491%22%2C%2260502%22%2C%2260503%22%2C%2260504%22%2C%2260505%22%2C%2260506%22%2C%2260510%22%2C%2260512%22%2C%2260514%22%2C%2260515%22%2C%2260516%22%2C%2260517%22%2C%2260532%22%2C%2260538%22%2C%2260540%22%2C%2260543%22%2C%2260544%22%2C%2260545%22%2C%2260551%22%2C%2260554%22%2C%2260555%22%2C%2260559%22%2C%2260560%22%2C%2260561%22%2C%2260563%22%2C%2260564%22%2C%2260565%22%2C%2260585%22%2C%2260586%22%2C%2299999%22%5D%2C%22SelectedSourceCodes%22%3A%5B%22-1%22%2C%22105%22%2C%2279%22%2C%2285%22%2C%2274%22%2C%2248%22%2C%22112%22%2C%22119%22%2C%22125%22%2C%2293%22%2C%22130%22%2C%2269%22%2C%2263%22%2C%22127%22%2C%2266%22%2C%2265%22%2C%2256%22%2C%228%22%2C%2247%22%2C%2276%22%2C%2290%22%2C%22123%22%2C%2282%22%2C%2294%22%2C%22124%22%2C%2258%22%2C%2271%22%2C%22107%22%2C%2255%22%2C%22101%22%2C%22102%22%2C%22132%22%2C%22122%22%2C%22118%22%2C%2273%22%2C%2254%22%2C%2253%22%2C%2250%22%2C%2284%22%2C%2218%22%2C%2223%22%2C%2243%22%2C%2283%22%2C%2236%22%2C%2224%22%2C%2230%22%2C%2226%22%2C%2233%22%2C%2232%22%2C%2231%22%2C%2277%22%2C%22128%22%2C%2217%22%2C%22115%22%2C%22106%22%2C%22111%22%2C%22113%22%2C%2251%22%2C%2297%22%2C%2299%22%2C%2298%22%2C%2296%22%2C%2245%22%2C%2295%22%2C%22108%22%2C%22121%22%2C%2249%22%2C%22126%22%2C%22114%22%2C%22117%22%2C%2292%22%2C%2281%22%2C%2260%22%2C%2212%22%2C%2222%22%2C%2211%22%2C%2235%22%2C%2237%22%2C%22116%22%2C%2210%22%2C%22131%22%2C%2288%22%2C%2289%22%2C%2219%22%2C%2234%22%2C%229%22%2C%2278%22%2C%2264%22%2C%2244%22%2C%2280%22%2C%2275%22%2C%2286%22%2C%2259%22%2C%2270%22%2C%22129%22%2C%2291%22%2C%22100%22%2C%22120%22%2C%22103%22%2C%2246%22%2C%2240%22%2C%2225%22%2C%2261%22%2C%22109%22%2C%2242%22%2C%2252%22%2C%2241%22%2C%2228%22%2C%22133%22%2C%22110%22%2C%227%22%2C%2257%22%2C%2262%22%2C%2287%22%2C%22104%22%5D%2C%22SelectedBillingTypes%22%3A%5B%22R%22%2C%22M%22%2C%22A%22%2C%22D%22%2C%22C%22%5D%2C%22SelectedCustomerStatuses%22%3A%5B%228%22%2C%229%22%5D%2C%22SelectedTaxCodes%22%3A%5B%5D%2C%22CustomerType%22%3A%22Both%22%2C%22BillToOption%22%3A%22All%22%2C%22Season%22%3A%222018%22%2C%22SelectedPropertyInventory%22%3A%220%22%2C%22StartDate%22%3A%221753-01-01+00%3A00%3A00%22%2C%22EndDate%22%3A%229998-12-31+23%3A59%3A59%22%2C%22DateRangeText%22%3A%22All%22%2C%22BeginCustomerNumberRange%22%3A%220%22%2C%22EndCustomerNumberRange%22%3A%22999999999%22%2C%22BeginMapcodeRange%22%3A%22%22%2C%22EndMapcodeRange%22%3A%22ZZ-ZZ-ZZZ%22%2C%22BeginCarrierRouteRange%22%3A%22%22%2C%22EndCarrierRouteRange%22%3A%22ZZZZ%22%2C%22BeginSizeRange%22%3A%220%22%2C%22EndSizeRange%22%3A%229999999.99%22%2C%22IncludeCustomersWithoutServices%22%3A%22true%22%2C%22SelectedSortOrder%22%3A%22CustomerNumber%22%2C%22SummaryOnly%22%3A%22false%22%2C%22PreviewFlag%22%3A%22false%22%2C%22SelectedFlags%22%3A%22%7B%5C%22with%5C%22%3A%5B%5D%2C%5C%22without%5C%22%3A%5B%5D%7D%22%2C%22AdvanceOptionsDataAsString%22%3A%22%7B%5C%22CustomerPhone%5C%22%3A0%2C%5C%22CustomerEmail%5C%22%3A0%2C%5C%22CustomerCreditHold%5C%22%3A0%2C%5C%22GroupBillingMasterAccounts%5C%22%3A%5B%5D%2C%5C%22WithDocuments%5C%22%3A%5B%5D%2C%5C%22WithoutDocuments%5C%22%3A%5B%5D%2C%5C%22DocumentsStartDate%5C%22%3A%5C%2201%2F01%2F2018%5C%22%2C%5C%22DocumentsEndDate%5C%22%3A%5C%2212%2F31%2F2018%5C%22%2C%5C%22DocumentsDateName%5C%22%3A%5C%22This+year%5C%22%2C%5C%22WithLetters%5C%22%3A%5B%5D%2C%5C%22WithoutLetters%5C%22%3A%5B%5D%2C%5C%22LettersStartDate%5C%22%3A%5C%2201%2F01%2F2018%5C%22%2C%5C%22LettersEndDate%5C%22%3A%5C%2212%2F31%2F2018%5C%22%2C%5C%22LettersDateName%5C%22%3A%5C%22This+year%5C%22%2C%5C%22WithNPS%5C%22%3A%5B%5D%2C%5C%22WithoutNPS%5C%22%3A%5B%5D%2C%5C%22NPSStartDate%5C%22%3A%5C%2201%2F01%2F2018%5C%22%2C%5C%22NPSEndDate%5C%22%3A%5C%2212%2F31%2F2018%5C%22%2C%5C%22NPSDateName%5C%22%3A%5C%22This+year%5C%22%2C%5C%22DontTelemarket%5C%22%3A%5C%220%5C%22%2C%5C%22DontDirectMail%5C%22%3A%5C%220%5C%22%2C%5C%22DontEmail%5C%22%3A%5C%220%5C%22%2C%5C%22DontFollowupByEmail%5C%22%3A%5C%220%5C%22%2C%5C%22DontUpsell%5C%22%3A%5C%220%5C%22%2C%5C%22NoKnock%5C%22%3A%5C%220%5C%22%2C%5C%22CawRegisteredUser%5C%22%3A%5C%220%5C%22%2C%5C%22AutoPay%5C%22%3A%5C%220%5C%22%2C%5C%22PreferredLanguage%5C%22%3A%5C%220%5C%22%2C%5C%22EmailStatements%5C%22%3A%5C%220%5C%22%2C%5C%22EmailPrenotification%5C%22%3A%5C%220%5C%22%2C%5C%22TextPrenotification%5C%22%3A%5C%220%5C%22%2C%5C%22WithContactPreferred%5C%22%3A%5B%5D%2C%5C%22WithoutContactPreferred%5C%22%3A%5B%5D%2C%5C%22WithSourceSize%5C%22%3A%5B%5D%2C%5C%22WithoutSourceSize%5C%22%3A%5B%5D%2C%5C%22WithSubdivisions%5C%22%3A%5B%5D%2C%5C%22WithoutSubdivisions%5C%22%3A%5B%5D%7D%22%2C%22SendToCallLog%22%3A%22false%22%2C%22SendToCallLogData%22%3Anull%2C%22MainGrouping%22%3A%22Ignore%22%2C%22SubGrouping%22%3A%22Ignore%22%2C%22AllRouteCodes%22%3A%22true%22%2C%22AllSourceCodes%22%3A%22false%22%2C%22AllZipCodes%22%3A%22true%22%2C%22AllTerritoryCodes%22%3A%22true%22%2C%22AllTaxCodes%22%3A%22true%22%2C%22ExportToCSV%22%3Atrue%7D"
}
]
}
},
更新12/4/2018 3:48 pm-我将代码更改为通过JSON进行URL编码:
var myContent = HttpUtility.UrlEncode(JsonConvert.SerializeObject(new model()));
var formUrlEncodedContent = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("model", myContent) });
var reportStep1Result = client.PostAsync("/my-folder/generate-report", formUrlEncodedContent);
reportStep1Result.Result.EnsureSuccessStatusCode();
var result = reportStep1Result.Result.Content.ReadAsStringAsync();
这是我得到的新错误:
{
"Success":false,
"Message":"Unexpected character encountered while parsing value: %. Path '', line 0, position 0.",
"Html":null,
"Model":null
}
URL编码的JSON如何在请求中出现两次?一旦它显示为“文本”的值并以model =作为前缀,并再次在“ params”数组中作为不带model =前缀的参数“ value”?
更新12/4/2018 4:03 pm-使用Fiddler进行跟踪显示:
从C#调用Fiddler进行检查时显示以下内容:
POST
https://example.com/my-folder/generate-report HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Host: example.com
Cookie: ASP.NET_SessionId=...;
SessionKey=...;
Emp_Id=...;
Expiration=12/4/2018 11:57:38 PM;
TimeClockSessionKey=...;
TimeClockEmp_Id=JJB
Content-Length: 7658
Expect: 100-continue
model=%257b%2522LoggedInEmployeeId%2522...
请注意,Fiddler版本以%25开头。浏览器一以%7b开头。 %25是%的URL编码-看来它的URL编码是2倍。我将其更改为StringContent而不是FormUrlEncodedContent,并手动仅对JSON进行编码,看看是否可行。...
我使用双URL编码解决了此问题。现在,该模型在Web浏览器和c#调用上均相等。仍然出现错误。两个文件头的标题不同。
来自C#控制台应用程序的标题:
POST
https://example.com/my-folder/generate-report HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Host: example.com
Cookie: ASP.NET_SessionId=...;
SessionKey=...;
Emp_Id=...;
Expiration=12/5/2018 3:04:35 AM;
TimeClockSessionKey=...;
TimeClockEmp_Id=...
Content-Length: 5386
Expect: 100-continue
浏览器头:
POST https://example.com/my-folder/generate-report HTTP/1.1
Host: example.com
Connection: keep-alive
Content-Length: 5385
Accept: application/json, text/javascript, */*; q=0.01
Origin: https://example.com
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: https://example.com/my-folder/generate-report/
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Cookie: _ga=...;
SearchView=grid;
HASH_SearchView=...;
_gid=...;
ASP.NET_SessionId=...;
LoginRedirectTo=https%3A%2F%2Fexample.com%2Fmy-folder%2Fgenerate-report%2F;
_gat_tracker1=1;
_gat_tracker2=1;
Emp_Id=...;
TimeClockSessionKey=...;
TimeClockEmp_Id=...;
Expiration=12/5/2018 3:06:22 AM;
SessionKey=...