HttpClient模拟客户端请求实例

HttpClient Get请求:

/// <summary>
        /// Get请求模拟
        /// </summary>
        /// <param name="url">请求URL</param>
        public void HttpRequest(string url)
        {
            HttpClient client = new HttpClient();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            Task<HttpResponseMessage> response = client.GetAsync(url);
            HttpResponseMessage result = response.Result;
            this.queryResultPane.Text = result.Content.ReadAsStringAsync().Result;
        }

HttpClient Post请求:

var aaa = new
            {
                Code = "139357a4-b38c-483e-a530-8f044a3dfe1c",
                WechatOpenID = "33265198",
                Area = "ShangHai",
                Birthday = DateTime.Parse("1986-8-18"),
                Gender = "male",
                Level = 80,
                Name = "Lynn",
                PhoneNumber = "13129623023",
                EmailType = "qq",
                IsHaveParent = "no",
                Province = "河南省",
                TheCity = "郑州市",
                //Age = 100,
                Labels = new List<LabelEntity>() { new LabelEntity() { Label = "uuuu" }, new LabelEntity() { Label = "yyyy" } }
            };

HttpClient client = new HttpClient();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            //简单类型的参数,webApi方法里可以不定义参数,通过HttpContext.Current.Request.QueryString["Mobile"]来获取
            Task<HttpResponseMessage> response =
                //client.PostAsJsonAsync<ThreeMMember>(string.Format(U8688 + "MemberPoint/UpdateMemberInfo"), member);
                client.PostAsJsonAsync(string.Format(U8688 + "MemberPoint/SaveMember"), aaa);
            HttpResponseMessage result = response.Result;
            this.Response.Output.Write(result.Content.ReadAsStringAsync().Result);

时间: 2024-10-07 06:23:47

HttpClient模拟客户端请求实例的相关文章

关于HttpClient模拟浏览器请求的參数乱码问题解决方式

转载请注明出处:http://blog.csdn.net/xiaojimanman/article/details/44407297 http://www.llwjy.com/blogdetail/9383e88e4bc7378b8318e15b0ac33559.html 个人博客站已经上线了,网址:www.llwjy.com,欢迎大家吐槽~ -----------------------------------------------------------------------------

关于HttpClient模拟浏览器请求的参数乱码问题解决方案

转载请注明出处:http://blog.csdn.net/xiaojimanman/article/details/44407297 http://www.llwjy.com/blogdetail/9383e88e4bc7378b8318e15b0ac33559.html 个人博客站已经上线了,网址:www.llwjy.com,欢迎大家吐槽~ -----------------------------------------------------------------------------

Node.js创建服务器和模拟客户端请求

1. 何为服务器 服务器是某种长期运行,等待请求资源的应用程序 2. 常见Web应用架构 3. 如何创建web服务器 Web服务器是使用HTTP协议,等待客户端连接后请求资源的驻守应用程序:HTTP协议是应用层的协议,在传输层依然是使用TCP或者UDP协议,一般来说是使用Socket来绑定TCP或者UDP,总的来说创建服务器就是创建一个Socket: 创建服务器的流程: (1)创建Socket (2)为Socket绑定参数 (3)Socket等候请求 (4)处理请求,返回资源 (5)关闭资源 4

HttpClient模拟http请求

最近工作中使用到了两个jar包 httpclient.jar, httpcore.jar HttpClient 的 abort(终止)程序示例 [java] view plaincopyprint? /* * ==================================================================== * Licensed to the Apache Software Foundation (ASF) under one * or more contr

httpclient模拟服务器请求

// 创建默认的httpClient实例. CloseableHttpClient httpclient = HttpClients.createDefault(); // 创建httppost HttpPost httppost = //new HttpPost("http://crmetl.yishenghuo.com:8142/outserver/startUpByHand.htm"); new HttpPost("http://localhost:8087/outse

CURL模拟POST请求实例

我们都知道可以通过表单的POST方法来提交数据给服务器,如图: 然后,testPost.php的代码比较简单.只是将接受到的内容打印. 得到这样的结果: 但是,我们能不能通过其他方式来提交数据给到testPost.php呢.是可以的!那就是使用CURL模拟POST提交数据.文件(curlPost.php)代码如图: 最后,我们来访问curlPost.php,也是可以得到相同的数据:

httpclient模拟post请求json封装表单数据

1 public static String httpPostWithJSON(String url) throws Exception { 2 3 HttpPost httpPost = new HttpPost(url); 4 CloseableHttpClient client = HttpClients.createDefault(); 5 String respContent = null; 6 7 // json方式 8 JSONObject jsonParam = new JSON

使用HttpClient模拟登录人人网+发状态+日志+分享+留言

事先使用HttpAnalyzer对人人网抓包观察,得到相关事件的数据.然后使用HttpClient模拟http请求相关操作.以下代码是今天刚测试过.欢迎新手拿去玩玩.注意:首先要在工程里导入httpclient的包,在apache官网上应该有的下载: http://hc.apache.org/downloads.cgi,目前是4.0+版本:其次记得将主函数里的账号和密码改为自己的,即可完成. import java.io.IOException; import java.io.Unsupport

java模拟post请求发送json

java模拟post请求发送json,用两种方式实现,第一种是HttpURLConnection发送post请求,第二种是使用httpclient模拟post请求, 方法一: 1 package main.utils; 2 3 import java.io.*; 4 import java.net.HttpURLConnection; 5 import java.net.URL; 6 7 public class HttpUtilTest { 8 Log log = new Log(this.g