从Request.Form

时间:2019-07-12 09:24:32

标签: c# forms http stream request

我有一个HTTP函数,它通过RequestStream向我自己的localhost发送字符串数据:

[Route("Test")]
public IActionResult Test()
{
     string data = "[0].meetingDate=2019-07-12&[0].courseId=12&[0].raceNumber=1&[0].horseCode=000000331213&[1].meetingDate=2019-07-12&[1].courseId=12&[1].raceNumber=1&[1].horseCode=000000356650&[2].meetingDate=2019-07-12&[2].courseId=50&[2].raceNumber=1&[2].horseCode=000000386467&[3].meetingDate=2019-07-12&[3].courseId=50&[3].raceNumber=1&[3].horseCode=000000389014&[4].meetingDate=2019-07-12&[4].courseId=12&[4].raceNumber=1&[4].horseCode=000000391778&[5].meetingDate=2019-07-12&[5].courseId=11&[5].raceNumber=1&[5].horseCode=000000393225&[6].meetingDate=2019-07-12&[6].courseId=50&[6].raceNumber=1&[6].horseCode=000000394338&[7].meetingDate=2019-07-12&[7].courseId=11&[7].raceNumber=1&[7].horseCode=000000395332&[8].meetingDate=2019-07-12&[8].courseId=12&[8].raceNumber=1&[8].horseCode=000000401021&[9].meetingDate=2019-07-12&[9].courseId=12&[9].raceNumber=1&[9].horseCode=000000403506&[10].meetingDate=2019-07-12&[10].courseId=12&[10].raceNumber=1&[10].horseCode=000000436835&[11].meetingDate=2019-07-12&[11].courseId=12&[11].raceNumber=1&[11].horseCode=000000443173&[12].meetingDate=2019-07-12&[12].courseId=12&[12].raceNumber=1&[12].horseCode=000000445131&[13].meetingDate=2019-07-12&[13].courseId=11&[13].raceNumber=1&[13].horseCode=000000451726&[14].meetingDate=2019-07-12&[14].courseId=12&[14].raceNumber=1&[14].horseCode=000000452502&[15].meetingDate=2019-07-12&[15].courseId=218&[15].raceNumber=1&[15].horseCode=000000454064&[16].meetingDate=2019-07-12&[16].courseId=12&[16].raceNumber=1&[16].horseCode=000000454106&[17].meetingDate=2019-07-12&[17].courseId=50&[17].raceNumber=1&[17].horseCode=000000457624&[18].meetingDate=2019-07-12&[18].courseId=12&[18].raceNumber=1&[18].horseCode=000000457889&[19].meetingDate=2019-07-12&[19].courseId=50&[19].raceNumber=1&[19].horseCode=000000458900&[20].meetingDate=2019-07-12&[20].courseId=218&[20].raceNumber=1&[20].horseCode=000000460767&[21].meetingDate=2019-07-12&[21].courseId=218&[21].raceNumber=1&[21].horseCode=000000462120&[22].meetingDate=2019-07-12&[22].courseId=218&[22].raceNumber=1&[22].horseCode=000000462476&[23].meetingDate=2019-07-12&[23].courseId=218&[23].raceNumber=1&[23].horseCode=000000463647&[24].meetingDate=2019-07-12&[24].courseId=218&[24].raceNumber=1&[24].horseCode=000000464200&[25].meetingDate=2019-07-12&[25].courseId=218&[25].raceNumber=1&[25].horseCode=000000464790&[26].meetingDate=2019-07-12&[26].courseId=11&[26].raceNumber=1&[26].horseCode=000000465029&[27].meetingDate=2019-07-12&[27].courseId=218&[27].raceNumber=1&[27].horseCode=000000465304&[28].meetingDate=2019-07-12&[28].courseId=11&[28].raceNumber=1&[28].horseCode=000000470349&[29].meetingDate=2019-07-12&[29].courseId=50&[29].raceNumber=1&[29].horseCode=000000471116&[30].meetingDate=2019-07-12&[30].courseId=11&[30].raceNumber=1&[30].horseCode=000000473061&[31].meetingDate=2019-07-12&[31].courseId=11&[31].raceNumber=1&[31].horseCode=000000475426&[32].meetingDate=2019-07-12&[32].courseId=218&[32].raceNumber=1&[32].horseCode=000000475820&[33].meetingDate=2019-07-12&[33].courseId=218&[33].raceNumber=1&[33].horseCode=000000475881&[34].meetingDate=2019-07-12&[34].courseId=60&[34].raceNumber=1&[34].horseCode=000000476240&[35].meetingDate=2019-07-12&[35].courseId=60&[35].raceNumber=1&[35].horseCode=000000476309&[36].meetingDate=2019-07-12&[36].courseId=218&[36].raceNumber=1&[36].horseCode=000000477122&[37].meetingDate=2019-07-12&[37].courseId=11&[37].raceNumber=1&[37].horseCode=000000478405&[38].meetingDate=2019-07-12&[38].courseId=218&[38].raceNumber=1&[38].horseCode=000000478410&[39].meetingDate=2019-07-12&[39].courseId=218&[39].raceNumber=1&[39].horseCode=000000478531&[40].meetingDate=2019-07-12&[40].courseId=11&[40].raceNumber=1&[40].horseCode=000000478672&[41].meetingDate=2019-07-12&[41].courseId=11&[41].raceNumber=1&[41].horseCode=000000478680&[42].meetingDate=2019-07-12&[42].courseId=218&[42].raceNumber=1&[42].horseCode=000000479871&[43].meetingDate=2019-07-12&[43].courseId=60&[43].raceNumber=1&[43].horseCode=000000480888&[44].meetingDate=2019-07-12&[44].courseId=12&[44].raceNumber=1&[44].horseCode=000000480891&[45].meetingDate=2019-07-12&[45].courseId=218&[45].raceNumber=1&[45].horseCode=000000482154&[46].meetingDate=2019-07-12&[46].courseId=60&[46].raceNumber=1&[46].horseCode=000000483012&[47].meetingDate=2019-07-12&[47].courseId=60&[47].raceNumber=1&[47].horseCode=000000483349&[48].meetingDate=2019-07-12&[48].courseId=60&[48].raceNumber=1&[48].horseCode=000000483781&[49].meetingDate=2019-07-12&[49].courseId=60&[49].raceNumber=1&[49].horseCode=000000484345&[50].meetingDate=2019-07-12&[50].courseId=60&[50].raceNumber=1&[50].horseCode=000000484589&[51].meetingDate=2019-07-12&[51].courseId=60&[51].raceNumber=1&[51].horseCode=000000485634&[52].meetingDate=2019-07-12&[52].courseId=11&[52].raceNumber=1&[52].horseCode=000000486370&[53].meetingDate=2019-07-12&[53].courseId=60&[53].raceNumber=1&[53].horseCode=000000487442&[54].meetingDate=2019-07-12&[54].courseId=60&[54].raceNumber=1&[54].horseCode=000000487999&[55].meetingDate=2019-07-12&[55].courseId=60&[55].raceNumber=1&[55].horseCode=000000488072&[56].meetingDate=2019-07-12&[56].courseId=60&[56].raceNumber=1&[56].horseCode=000000489640&[57].meetingDate=2019-07-12&[57].courseId=11&[57].raceNumber=1&[57].horseCode=000000496740&[58].meetingDate=2019-07-12&[58].courseId=218&[58].raceNumber=1&[58].horseCode=000000497795&[59].meetingDate=2019-07-12&[59].courseId=60&[59].raceNumber=1&[59].horseCode=000000497976&[60].meetingDate=2019-07-12&[60].courseId=1&[60].raceNumber=1&[60].horseCode=000000498540&[61].meetingDate=2019-07-12&[61].courseId=218&[61].raceNumber=1&[61].horseCode=000000498569&[62].meetingDate=2019-07-12&[62].courseId=60&[62].raceNumber=1&[62].horseCode=000000498780&[63].meetingDate=2019-07-12&[63].courseId=1&[63].raceNumber=1&[63].horseCode=000000500483&[64].meetingDate=2019-07-12&[64].courseId=218&[64].raceNumber=1&[64].horseCode=000000500824&[65].meetingDate=2019-07-12&[65].courseId=60&[65].raceNumber=1&[65].horseCode=000000501211&[66].meetingDate=2019-07-12&[66].courseId=1&[66].raceNumber=1&[66].horseCode=000000501625&[67].meetingDate=2019-07-12&[67].courseId=218&[67].raceNumber=1&[67].horseCode=000000502612&[68].meetingDate=2019-07-12&[68].courseId=1&[68].raceNumber=1&[68].horseCode=000000502638&[69].meetingDate=2019-07-12&[69].courseId=218&[69].raceNumber=1&[69].horseCode=000000503245&[70].meetingDate=2019-07-12&[70].courseId=62&[70].raceNumber=1&[70].horseCode=000000503436&[71].meetingDate=2019-07-12&[71].courseId=62&[71].raceNumber=1&[71].horseCode=000000503776&[72].meetingDate=2019-07-12&[72].courseId=218&[72].raceNumber=1&[72].horseCode=000000503871&[73].meetingDate=2019-07-12&[73].courseId=1&[73].raceNumber=1&[73].horseCode=000000504536&[74].meetingDate=2019-07-12&[74].courseId=62&[74].raceNumber=1&[74].horseCode=000000505180&[75].meetingDate=2019-07-12&[75].courseId=1&[75].raceNumber=1&[75].horseCode=000000505588&[76].meetingDate=2019-07-12&[76].courseId=62&[76].raceNumber=1&[76].horseCode=000000505720&[77].meetingDate=2019-07-12&[77].courseId=218&[77].raceNumber=1&[77].horseCode=000000505995&[78].meetingDate=2019-07-12&[78].courseId=218&[78].raceNumber=1&[78].horseCode=000000505997&[79].meetingDate=2019-07-12&[79].courseId=218&[79].raceNumber=1&[79].horseCode=000000506773&[80].meetingDate=2019-07-12&[80].courseId=218&[80].raceNumber=1&[80].horseCode=000000506786&[81].meetingDate=2019-07-12&[81].courseId=218&[81].raceNumber=1&[81].horseCode=000000508782&[82].meetingDate=2019-07-12&[82].courseId=62&[82].raceNumber=1&[82].horseCode=000000508900&[83].meetingDate=2019-07-12&[83].courseId=218&[83].raceNumber=1&[83].horseCode=000000508935&[84].meetingDate=2019-07-12&[84].courseId=62&[84].raceNumber=2&[84].horseCode=000000336492&[85].meetingDate=2019-07-12&[85].courseId=62&[85].raceNumber=2&[85].horseCode=000000338836&[86].meetingDate=2019-07-12&[86].courseId=60&[86].raceNumber=3&[86].horseCode=000000362848&[87].meetingDate=2019-07-12&[87].courseId=50&[87].raceNumber=2&[87].horseCode=000000372908&[88].meetingDate=2019-07-12&[88].courseId=12&[88].raceNumber=3&[88].horseCode=000000374411&[89].meetingDate=2019-07-12&[89].courseId=50&[89].raceNumber=2&[89].horseCode=000000381245&[90].meetingDate=2019-07-12&[90].courseId=11&[90].raceNumber=2&[90].horseCode=000000381599&[91].meetingDate=2019-07-12&[91].courseId=12&[91].raceNumber=3&[91].horseCode=000000383914&[92].meetingDate=2019-07-12&[92].courseId=60&[92].raceNumber=3&[92].horseCode=000000387850&[93].meetingDate=2019-07-12&[93].courseId=218&[93].raceNumber=3&[93].horseCode=000000388082&[94].meetingDate=2019-07-12&[94].courseId=218&[94].raceNumber=3&[94].horseCode=000000388214&[95].meetingDate=2019-07-12&[95].courseId=1&[95].raceNumber=3&[95].horseCode=000000388248&[96].meetingDate=2019-07-12&[96].courseId=60&[96].raceNumber=3&[96].horseCode=000000388584&[97].meetingDate=2019-07-12&[97].courseId=50&[97].raceNumber=2&[97].horseCode=000000394261&[98].meetingDate=2019-07-12&[98].courseId=62&[98].raceNumber=2&[98].horseCode=000000396607&[99].meetingDate=2019-07-12&[99].courseId=11&[99].raceNumber=3&[99].horseCode=000000398211&[100].meetingDate=2019-07-12&[100].courseId=218&[100].raceNumber=3&[100].horseCode=000000398668&[101].meetingDate=2019-07-12&[101].courseId=60&[101].raceNumber=3&[101].horseCode=000000400436&[102].meetingDate=2019-07-12&[102].courseId=60&[102].raceNumber=3&[102].horseCode=000000400454&[103].meetingDate=2019-07-12&[103].courseId=11&[103].raceNumber=3&[103].horseCode=000000402677&[104].meetingDate=2019-07-12&[104].courseId=50&[104].raceNumber=3&[104].horseCode=000000404426&[105].meetingDate=2019-07-12&[105].courseId=50&[105].raceNumber=2&[105].horseCode=000000405879&[106].meetingDate=2019-07-12&[106].courseId=218&[106].raceNumber=3&[106].horseCode=000000410361&[107].meetingDate=2019-07-12&[107].courseId=50&[107].raceNumber=3&[107].horseCode=000000411965&[108].meetingDate=2019-07-12&[108].courseId=218&[108].raceNumber=3&[108].horseCode=000000412388&[109].meetingDate=2019-07-12&[109].courseId=218&[109].raceNumber=3&[109].horseCode=000000435853&[110].meetingDate=2019-07-12&[110].courseId=50&[110].raceNumber=3&[110].horseCode=000000436920&[111].meetingDate=2019-07-12&[111].courseId=62&[111].raceNumber=3&[111].horseCode=000000439805&[112].meetingDate=2019-07-12&[112].courseId=60&[112].raceNumber=3&[112].horseCode=000000440534&[113].meetingDate=2019-07-12&[113].courseId=218&[113].raceNumber=2&[113].horseCode=000000440733&[114].meetingDate=2019-07-12&[114].courseId=60&[114].raceNumber=3&[114].horseCode=000000440735&[115].meetingDate=2019-07-12&[115].courseId=62&[115].raceNumber=2&[115].horseCode=000000441436&[116].meetingDate=2019-07-12&[116].courseId=60&[116].raceNumber=3&[116].horseCode=000000443717&[117].meetingDate=2019-07-12&[117].courseId=60&[117].raceNumber=3&[117].horseCode=000000444158&[118].meetingDate=2019-07-12&[118].courseId=11&[118].raceNumber=3&[118].horseCode=000000444404&[119].meetingDate=2019-07-12&[119].courseId=11&[119].raceNumber=2&[119].horseCode=000000444528&[120].meetingDate=2019-07-12&[120].courseId=218&[120].raceNumber=2&[120].horseCode=000000444917&[121].meetingDate=2019-07-12&[121].courseId=62&[121].raceNumber=2&[121].horseCode=000000445263&[122].meetingDate=2019-07-12&[122].courseId=12&[122].raceNumber=3&[122].horseCode=000000447414&[123].meetingDate=2019-07-12&[123].courseId=11&[123].raceNumber=2&[123].horseCode=000000447424&[124].meetingDate=2019-07-12&[124].courseId=218&[124].raceNumber=2&[124].horseCode=000000448866&[125].meetingDate=2019-07-12&[125].courseId=218&[125].raceNumber=3&[125].horseCode=000000449659&[126].meetingDate=2019-07-12&[126].courseId=218&[126].raceNumber=2&[126].horseCode=000000450158&[127].meetingDate=2019-07-12&[127].courseId=1&[127].raceNumber=3&[127].horseCode=000000451016&[128].meetingDate=2019-07-12&[128].courseId=62&[128].raceNumber=2&[128].horseCode=000000451420&[129].meetingDate=2019-07-12&[129].courseId=218&[129].raceNumber=2&[129].horseCode=000000452360&[130].meetingDate=2019-07-12&[130].courseId=1&[130].raceNumber=3&[130].horseCode=000000452492&[131].meetingDate=2019-07-12&[131].courseId=11&[131].raceNumber=3&[131].horseCode=000000453151&[132].meetingDate=2019-07-12&[132].courseId=50&[132].raceNumber=2&[132].horseCode=000000453261&[133].meetingDate=2019-07-12&[133].courseId=50&[133].raceNumber=3&[133].horseCode=000000453308&[134].meetingDate=2019-07-12&[134].courseId=62&[134].raceNumber=3&[134].horseCode=000000454081&[135].meetingDate=2019-07-12&[135].courseId=62&[135].raceNumber=3&[135].horseCode=000000457607&[136].meetingDate=2019-07-12&[136].courseId=60&[136].raceNumber=3&[136].horseCode=000000457746&[137].meetingDate=2019-07-12&[137].courseId=62&[137].raceNumber=2&[137].horseCode=000000457923&[138].meetingDate=2019-07-12&[138].courseId=218&[138].raceNumber=3&[138].horseCode=000000458014&[139].meetingDate=2019-07-12&[139].courseId=12&[139].raceNumber=3&[139].horseCode=000000458548&[140].meetingDate=2019-07-12&[140].courseId=218&[140].raceNumber=3&[140].horseCode=000000460158&[141].meetingDate=2019-07-12&[141].courseId=50&[141].raceNumber=2&[141].horseCode=000000460346&[142].meetingDate=2019-07-12&[142].courseId=218&[142].raceNumber=3&[142].horseCode=000000460962&[143].meetingDate=2019-07-12&[143].courseId=60&[143].raceNumber=3&[143].horseCode=000000461822&[144].meetingDate=2019-07-12&[144].courseId=1&[144].raceNumber=3&[144].horseCode=000000462382&[145].meetingDate=2019-07-12&[145].courseId=11&[145].raceNumber=2&[145].horseCode=000000462485&[146].meetingDate=2019-07-12&[146].courseId=11&[146].raceNumber=3&[146].horseCode=000000462947&[147].meetingDate=2019-07-12&[147].courseId=62&[147].raceNumber=3&[147].horseCode=000000463020&[148].meetingDate=2019-07-12&[148].courseId=1&[148].raceNumber=3&[148].horseCode=000000463435&[149].meetingDate=2019-07-12&[149].courseId=60&[149].raceNumber=3&[149].horseCode=000000463501&[150].meetingDate=2019-07-12&[150].courseId=62&[150].raceNumber=3&[150].horseCode=000000463806&[151].meetingDate=2019-07-12&[151].courseId=11&[151].raceNumber=2&[151].horseCode=000000464078&[152].meetingDate=2019-07-12&[152].courseId=62&[152].raceNumber=2&[152].horseCode=000000467081&[153].meetingDate=2019-07-12&[153].courseId=11&[153].raceNumber=2&[153].horseCode=000000467116&[154].meetingDate=2019-07-12&[154].courseId=50&[154].raceNumber=2&[154].horseCode=000000468703&[155].meetingDate=2019-07-12&[155].courseId=60&[155].raceNumber=3&[155].horseCode=000000470728&[156].meetingDate=2019-07-12&[156].courseId=218&[156].raceNumber=2&[156].horseCode=000000478316&[157].meetingDate=2019-07-12&[157].courseId=11&[157].raceNumber=2&[157].horseCode=000000479951&[158].meetingDate=2019-07-12&[158].courseId=1&[158].raceNumber=2&[158].horseCode=000000480031&[159].meetingDate=2019-07-12&[159].courseId=11&[159].raceNumber=2&[159].horseCode=000000480978&[160].meetingDate=2019-07-12&[160].courseId=1&[160].raceNumber=2&[160].horseCode=000000487823&[161].meetingDate=2019-07-12&[161].courseId=218&[161].raceNumber=2&[161].horseCode=000000487872&[162].meetingDate=2019-07-12&[162].courseId=218&[162].raceNumber=2&[162].horseCode=000000490708&[163].meetingDate=2019-07-12&[163].courseId=62&[163].raceNumber=2&[163].horseCode=000000490845&[164].meetingDate=2019-07-12&[164].courseId=218&[164].raceNumber=2&[164].horseCode=000000491327&[165].meetingDate=2019-07-12&[165].courseId=1&[165].raceNumber=2&[165].horseCode=000000492183&[166].meetingDate=2019-07-12&[166].courseId=12&[166].raceNumber=2&[166].horseCode=000000498399&[167].meetingDate=2019-07-12&[167].courseId=11&[167].raceNumber=2&[167].horseCode=000000500280&[168].meetingDate=2019-07-12&[168].courseId=1&[168].raceNumber=2&[168].horseCode=000000500606&[169].meetingDate=2019-07-12&[169].courseId=1&[169].raceNumber=2&[169].horseCode=000000501807&[170].meetingDate=2019-07-12&[170].courseId=60&[170].raceNumber=2&[170].horseCode=000000502541&[171].meetingDate=2019-07-12&[171].courseId=218&[171].raceNumber=2&[171].horseCode=000000503163&[172].meetingDate=2019-07-12&[172].courseId=60&[172].raceNumber=2&[172].horseCode=000000503666&[173].meetingDate=2019-07-12&[173].courseId=12&[173].raceNumber=2&[173].horseCode=000000504251&[174].meetingDate=2019-07-12&[174].courseId=60&[174].raceNumber=2&[174].horseCode=000000504505&[175].meetingDate=2019-07-12&[175].courseId=60&[175].raceNumber=2&[175].horseCode=000000504506&[176].meetingDate=2019-07-12&[176].courseId=60&[176].raceNumber=2&[176].horseCode=000000504508&[177].meetingDate=2019-07-12&[177].courseId=60&[177].raceNumber=2&[177].horseCode=000000505170&[178].meetingDate=2019-07-12&[178].courseId=218&[178].raceNumber=2&[178].horseCode=000000506326&[179].meetingDate=2019-07-12&[179].courseId=12&[179].raceNumber=2&[179].horseCode=000000506392&[180].meetingDate=2019-07-12&[180].courseId=12&[180].raceNumber=2&[180].horseCode=000000506394&[181].meetingDate=2019-07-12&[181].courseId=60&[181].raceNumber=2&[181].horseCode=000000506883&[182].meetingDate=2019-07-12&[182].courseId=12&[182].raceNumber=2&[182].horseCode=000000507087&[183].meetingDate=2019-07-12&[183].courseId=12&[183].raceNumber=2&[183].horseCode=000000507372&[184].meetingDate=2019-07-12&[184].courseId=12&[184].raceNumber=2&[184].horseCode=000000507406&[185].meetingDate=2019-07-12&[185].courseId=218&[185].raceNumber=2&[185].horseCode=000000508225";

    try
    {
        var req = WebRequest.Create("https://localhost:44374/HorseRacingApi/Prices/GetPriceForEntries");
        req.Method = "POST";

        // if( !string.IsNullOrEmpty(data))
        // {
        var dataBytes = Encoding.ASCII.GetBytes(data);
        req.ContentType = "application/x-www-form-urlencoded";

        req.ContentLength = dataBytes.Length;

        using (var requestDS = req.GetRequestStream())
        {
            requestDS.Write(dataBytes, 0, dataBytes.Length);
            requestDS.Flush();
        }
        // }

        HttpWebResponse response = (HttpWebResponse)req.GetResponse();
        Stream receiveStream = response.GetResponseStream();

        StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);

        Debug.WriteLine("Response stream received.");
        Debug.WriteLine(readStream.ReadToEnd());
        response.Close();
        readStream.Close();

        return Ok("Sent!");
    }
    catch(Exception ex)
    {
        throw ex;
    }
}

然后我从另一个有效的函数中读取表单data

[HttpPost]
[Route("GetPriceForEntries")]
public JsonResult GetPriceForEntries([FromForm] List<Core.Models.JsonEntryKey> Entries, bool? ShowAll)
{
    foreach(var data in Entries)
    {
        Debug.WriteLine(data.meetingDate + " - " + data.courseId);
    }

    return Json("GOT");
}

但是,data字符串可能会变得很长,我估计长度为47k,如果太长,则会收到HTTP 500异常错误。

我的问题是,有没有更好的方法来读取Request.Form数据? Test函数是旧软件中的一段代码,因此我无法真正更改其发送data的方式,只能读取它。

0 个答案:

没有答案