代码之家  ›  专栏  ›  技术社区  ›  Sumit raj

将Post-API调用参数获取为空

  •  0
  • Sumit raj  · 技术社区  · 6 年前

    我正试着用很大的参数来做一个后调用。但电话不通。

    发行 :当参数的大小为大时,不会进行调用。但如果参数较小,则一切正常,这意味着API正在工作。我试着研究 POST 打电话但没能成功。

    我所做的 我做了一个示例api项目,并尝试通过postman和project进行测试。

    邮递员电话 Postman call 这是工作正常,如预期。

    在项目中 enter image description here enter image description here

    如果有人要指出 rquest.ContentLength = 0; . 这是因为param只被附加到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)
    
    2 回复  |  直到 6 年前
        1
  •  2
  •   artista_14    6 年前
    // 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 完整的解释。

        2
  •  0
  •   Tanato    6 年前

    你试过用 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();