/// <summary> /// Retrives fraud level for given call data in json format. /// More information at: https://nextcaller.com/documentation/v2.1/#/fraud-levels/curl. /// </summary> /// <param name="callData">Call data to be posted.</param> /// <param name="accountId">Platform account ID.</param> /// <returns>Fraud level for given call data in json format.</returns> public string AnalyzeCallJson(string callData, string accountId = null) { var headers = PrepareAccountIdHeader(accountId); string url = BuildUrl(analyzeUrl, new UrlParameter(formatParameterName, PostContentType.ToString())); return(httpTransport.Request(url, PostContentType, "POST", callData ?? "", headers)); }
public async Task PostAsync_UnsupportedMediaType(PostContentType contentType) { // Arrange using var content = GetPostContent(contentType, _slime); using var request = new HttpRequestMessage(HttpMethod.Post, "/api/monster") { Content = content, }; // Act using var response = await SendAsync(request); var problem = await DeserializeAsync <ProblemDetails>(response); // Assert Assert.Equal(HttpStatusCode.UnsupportedMediaType, response.StatusCode); Assert.NotNull(problem); Assert.Equal((int)HttpStatusCode.UnsupportedMediaType, problem.Status.Value); }
public async Task PostAsync_Ok(PostContentType contentType) { // Arrange using var content = GetPostContent(contentType, _slime); using var request = new HttpRequestMessage(HttpMethod.Post, "/api/monster") { Content = content, }; // Act using var response = await SendAsync(request); var monster = await DeserializeAsync <Monster>(response); // Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(_slime.Id, monster.Id); Assert.Equal(_slime.Name, monster.Name); }
public async Task PostBodyJsonAsync_UnsupportedMediaType(PostContentType contentType) { // Arrange using var content = GetPostContent(contentType, _slime); using var request = new HttpRequestMessage(HttpMethod.Post, "/api/monster/body/json") { Content = content, }; // Act using var response = await SendAsync(request); var responseText = await response.Content?.ReadAsStringAsync(); // Assert Assert.Equal(HttpStatusCode.UnsupportedMediaType, response.StatusCode); Assert.NotNull(response.Content); Assert.NotNull(responseText); Assert.Equal(0, responseText.Length); }
private static HttpContent GetPostContent(PostContentType contentType, Monster monster) { switch (contentType) { case PostContentType.FormUrlEncoded: // application/x-www-form-urlencoded var formValues = new Dictionary <string, string> { { "id", monster.Id.ToString() }, { "name", monster.Name }, }; return(new FormUrlEncodedContent(formValues)); case PostContentType.JsonString: case PostContentType.JsonStringTextPlain: var content = GetJsonStringContent(monster); content.Headers.ContentType.MediaType = contentType == PostContentType.JsonString ? "application/json" : "text/plain"; return(content); } throw new ArgumentOutOfRangeException(nameof(contentType)); }
/// <summary> /// Updates profile. /// More info at: https://nextcaller.com/platform/documentation/v2.1/#/post-profile/curl. /// </summary> /// <param name="profileId">ID of the profile to be updated.</param> /// <param name="accountId">Platform account ID.</param> /// <param name="profileData">Profile data to be updated in json.</param> public void UpdateByProfileIdJson(string profileId, string profileData, string accountId) { var headers = PrepareAccountIdHeader(accountId); string url = BuildUrl(usersUrl + profileId, new UrlParameter(formatParameterName, PostContentType.ToString())); httpTransport.Request(url, PostContentType, "POST", profileData ?? "", headers); }
/// <summary> /// Updates platform account. /// More info at: https://nextcaller.com/platform/documentation/v2.1/#/accounts/put-platform-account/curl. /// </summary> /// <param name="accountId">Platform account ID to be updated.</param> /// <param name="accountData">Plaftorm account information in json.</param> public void UpdatePlatformAccountJson(string accountData, string accountId) { string url = BuildUrl(platformUrl + accountId, new UrlParameter(formatParameterName, PostContentType.ToString())); httpTransport.Request(url, PostContentType, "PUT", accountData ?? ""); }
/// <summary> /// http Post请求 /// </summary> /// <param name="requestUrl">请求url</param> /// <param name="objPostData">发送数据,form表单格式传字典,json格式传json文本,xml格式传xml文本</param> /// <param name="contentType">发送数据类型</param> /// <param name="headers">请求header信息</param> /// <param name="timeout">超时时间1000*90</param> /// <returns></returns> public string HttpPost(string requestUrl, object objPostData, PostContentType contentType, Dictionary <string, string> headers = null, int timeout = 90000) { if (string.IsNullOrEmpty(requestUrl)) { return(""); } HttpWebRequest webRequest = (WebRequest.Create(requestUrl) as HttpWebRequest); if (requestUrl.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { ServicePointManager.ServerCertificateValidationCallback = HttpHelper.CheckValidationResult; webRequest.ProtocolVersion = HttpVersion.Version10; } webRequest.Method = "POST"; webRequest.Timeout = timeout; //表单数据 if (contentType == PostContentType.Form) { Dictionary <string, string> parameters = objPostData as Dictionary <string, string>; if (!(parameters == null || parameters.Count == 0)) { webRequest.ContentType = "application/x-www-form-urlencoded"; StringBuilder buffer = new StringBuilder(); int i = 0; foreach (string key in parameters.Keys) { if (i > 0) { buffer.AppendFormat("&{0}={1}", key, HttpUtility.UrlEncode(parameters[key], Encoding.UTF8)); } else { buffer.AppendFormat("{0}={1}", key, HttpUtility.UrlEncode(parameters[key], Encoding.UTF8)); } i++; } byte[] data = Encoding.UTF8.GetBytes(buffer.ToString()); webRequest.ContentLength = data.Length; using (Stream stream = webRequest.GetRequestStream()) { stream.Write(data, 0, data.Length); } } } else if (contentType == PostContentType.Json)//json数据 { string jsonData = objPostData as string; if (!string.IsNullOrEmpty(jsonData)) { webRequest.ContentType = "application/json"; byte[] data = Encoding.UTF8.GetBytes(jsonData); webRequest.ContentLength = data.Length; using (Stream stream = webRequest.GetRequestStream()) { stream.Write(data, 0, data.Length); } } } else if (contentType == PostContentType.Xml)//xml数据 { string jsonData = objPostData as string; if (!string.IsNullOrEmpty(jsonData)) { webRequest.ContentType = "application/xml"; byte[] data = Encoding.UTF8.GetBytes(jsonData); webRequest.ContentLength = data.Length; using (Stream stream = webRequest.GetRequestStream()) { stream.Write(data, 0, data.Length); } } } if (headers != null) { foreach (KeyValuePair <string, string> header in headers) { webRequest.Headers.Add(header.Key, header.Value); } } string result = String.Empty; using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse()) { //如果webResponse.StatusCode的值为HttpStatusCode.OK,表示成功 using (Stream stream = webResponse.GetResponseStream()) { using (StreamReader streamReader = new StreamReader(stream, Encoding.UTF8)) { result = streamReader.ReadToEnd(); } } } return(result); }