获取 Post API 调用参数为 null

Getting Post API Call argument as null

我正在尝试使用非常大的参数大小进行 post 调用。但是电话打不通。

问题:当参数大小较大时,调用不会进行 through.But 如果参数较小,一切都很好,这意味着 API 正在工作。我试图研究 POST 调用,但没能成功。

我做了什么 我制作了一个示例 api 项目并尝试通过 postman 和项目对其进行测试。

邮递员电话 这按预期工作正常。

In-Project

如果有人要指出rquest.ContentLength = 0;。这是因为参数仅附加到 url。我不打算附加它但是 我不知道该怎么做,也用谷歌搜索。对于小长度,这是有效的,但如果字符串长度达到 3000,它就会失败。

API 呼叫发起人

UriBuilder uriBuilder = new UriBuilder(restURL);
var query = HttpUtility.ParseQueryString(uriBuilder.Query);
query["json"] = new JavaScriptSerializer().Serialize(reportParameterDictionary);
uriBuilder.Query = query.ToString();
restURL = uriBuilder.ToString();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(restURL);
request.Method = "Post";
request.ContentType = "application/json";
rquest.ContentLength = 0;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
  StreamReader reader = new StreamReader(response.GetResponseStream());
  string responseData = reader.ReadToEnd();
  response.Close();
  dynamic obj = JsonConvert.DeserializeObject(responseData);
  if (obj != null)
  {
   printresult = obj.Success;
  }
}

接收端

[Route("PrintReport")]
public IHttpActionResult PrintReport(string json)

您是否尝试过使用 HttpClient 而不是 HttpWebRequest

var client = new HttpClient();
var content = new StringContent("YOUR LONG STRING", 
                             System.Text.Encoding.Unicode, 
                             "application/json");            
var task = client.PostAsync(restURL, content);
var str = await task.Result.Content.ReadAsStringAsync();   
// Create a request using a URL that can receive a post.   
WebRequest request = WebRequest.Create ("http://www.contoso.com/PostAccepter.aspx");  
// Set the Method property of the request to POST.  
request.Method = "POST";  
// Create POST data and convert it to a byte array.  
string postData = "This is a test that posts this string to a Web server.";  
byte[] byteArray = Encoding.UTF8.GetBytes (postData);  
// Set the ContentType property of the WebRequest.  
request.ContentType = "application/x-www-form-urlencoded";  
// Set the ContentLength property of the WebRequest.  
request.ContentLength = byteArray.Length;  
// Get the request stream.  
Stream dataStream = request.GetRequestStream ();  
// Write the data to the request stream.  
dataStream.Write (byteArray, 0, byteArray.Length);  
// Close the Stream object.  
dataStream.Close ();  
// Get the response.  
WebResponse response = request.GetResponse ();  
// Display the status.  
Console.WriteLine (((HttpWebResponse)response).StatusDescription);  
// Get the stream containing content returned by the server.  
dataStream = response.GetResponseStream ();  
// Open the stream using a StreamReader for easy access.  
StreamReader reader = new StreamReader (dataStream);  
// Read the content.  
string responseFromServer = reader.ReadToEnd ();  
// Display the content.  
Console.WriteLine (responseFromServer);  
// Clean up the streams.  
reader.Close ();  
dataStream.Close ();  
response.Close ();  

按照此 Link 进行完整解释。