将Post API调用参数获取为null

时间:2018-07-27 03:43:17

标签: c# asp.net-web-api2 postman

我正在尝试以很大的参数大小进行发布。但是电话没有接通。

问题:当参数大时,不会进行调用。但是,对于小参数而言,一切都很好,这意味着API可以正常工作。我尝试研究有关import sys if __name__ == '__main__': args = sys.argv for key in args: print(key) 的通话,但无法正常进行。

我做了什么 我制作了一个示例api项目,并尝试通过邮递员和项目进行测试。

邮递员通话 Postman call 一切正常。

项目中 enter image description here enter image description here

如果有人要指出POST。这是因为参数仅被附加到URL。我不希望它被附加,但是 我不知道该怎么做,也用谷歌搜索。对于较小的长度,这是可行的,但要说如果字符串长度达到3000,则会失败。

API调用发起人

rquest.ContentLength = 0;

接收结束

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;
  }
}

2 个答案:

答案 0 :(得分:2)

// 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以获取完整说明。

答案 1 :(得分:0)

您是否尝试过使用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();