WebClient发送Post请求实例

WebClient发送Post请求实例

 

 

List<BaseModuleEntity> returnResult = new List<BaseModuleEntity>();
            try
            {
                string url = System.Configuration.ConfigurationManager.AppSettings["LogonService"] + "/PermissionService.ashx";
                
                WebClient webClient = new WebClient();
                NameValueCollection postValues = new NameValueCollection();
                postValues.Add("Function", "GetPermissionList");
                postValues.Add("UserInfo", userInfo.Serialize());
                postValues.Add("SystemCode", systemCode);
                postValues.Add("fromCache", false.ToString());
                // 向服务器发送POST数据
                byte[] responseArray = webClient.UploadValues(url, postValues);
                string response = Encoding.UTF8.GetString(responseArray);
                if (!string.IsNullOrEmpty(response))
                {
                    JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
                    returnResult = javaScriptSerializer.Deserialize<List<BaseModuleEntity>>(response);
                    returnResult = returnResult.OrderBy(t => t.SortCode).ToList();
                    HttpContext.Current.Session[permissionKey] = returnResult;
                }
            }
            catch (Exception ex)
            {
                Log.Write(ex.ToString());
            }
            return returnResult;

 

posted @ 2019-01-23 08:48 波霸38 阅读( ...) 评论( ...) 编辑 收藏
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个使用Spring WebClient进行异步POST请求的代码范例,带有注释解释每个步骤的作用: ```java import org.springframework.http.MediaType; import org.springframework.http.client.reactive.ReactorClientHttpConnector; import org.springframework.util.MultiValueMap; import org.springframework.web.reactive.function.BodyInserters; import org.springframework.web.reactive.function.client.ClientResponse; import org.springframework.web.reactive.function.client.WebClient; import reactor.core.publisher.Mono; import java.time.Duration; public class WebClientExample { public static void main(String[] args) { // 创建一个WebClient实例 WebClient webClient = WebClient.builder() // 指定请求超时时间 .clientConnector(new ReactorClientHttpConnector()) .defaultHeader("Content-Type", MediaType.APPLICATION_FORM_URLENCODED_VALUE) .defaultHeader("Accept", MediaType.APPLICATION_JSON_VALUE) .baseUrl("http://localhost:8080") .build(); // 构造请求参数 MultiValueMap<String, String> formData = new LinkedMultiValueMap<>(); formData.add("param1", "value1"); formData.add("param2", "value2"); // 发送POST请求 Mono<ClientResponse> responseMono = webClient.post() // 指定请求路径 .uri("/api/path") // 设置请求体 .body(BodyInserters.fromFormData(formData)) // 发送请求并返回响应结果 .exchange(); // 处理响应结果 responseMono.subscribe(response -> { // 打印响应状态码 System.out.println(response.statusCode()); // 打印响应头 response.headers().asHttpHeaders().forEach((name, values) -> { System.out.println(name + ": " + values); }); // 打印响应体 response.bodyToMono(String.class).subscribe(System.out::println); }); } } ``` 在这个例子中,我们创建了一个WebClient实例,并指定了请求超时时间、默认请求头、请求的基础URL等信息。然后,我们构造了一个包含请求参数的MultiValueMap对象,并将其作为请求发送POST请求WebClientpost()方法返回一个RequestHeadersSpec对象,该对象可以用于设置请求头、请求体等信息。在这个例子中,我们使用uri()方法指定了请求路径,并使用body()方法设置了请求体。 最后,我们调用exchange()方法发送请求并返回响应结果。exchange()方法返回一个Mono<ClientResponse>对象,我们可以使用subscribe()方法对其进行订阅,然后在回调函数中处理响应结果。在这个例子中,我们打印了响应状态码、响应头和响应体。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值