C#调用 webapi接口 利用HTTP头验证的问题

一直学习 2015-11-17 09:50:39


请求地址:http://192.168.0.124/testwebapi/api/AddUser

请求方式:Post

验证方式:用户名密码等信息放在Http Header 提交.

Account=用户名
pwd =密码
rid =3

参数示例:{"id": "123","name": "张建"}
参数说明:
字段参数类型参数说明参数范围说明
id string 编号
name string 名称

____________________________________________
怎么调用这个 webAPI接口??

...全文
11946 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
EnForGrass 2016-12-02
  • 打赏
  • 举报
回复
引用 5 楼 szjarvis 的回复:
HttpResponseMessage response = client.PostAsJsonAsync<Customer>(url, book).Result; 谁能解释一下这里的 book 是什么? 还有为什么我的client没有PostAsJsonAsync这玩意?
MVC4,client就是这样定义 HttpClient client = new HttpClient(); book就是一个实体对象,比如 var book = new Book(){ id=1, Name="Via C#", BookNo="No1"};
我2我骄傲 2016-12-01
  • 打赏
  • 举报
回复
引用 5 楼 szjarvis 的回复:
HttpResponseMessage response = client.PostAsJsonAsync<Customer>(url, book).Result; 谁能解释一下这里的 book 是什么? 还有为什么我的client没有PostAsJsonAsync这玩意?
引用System.Net.Http.Formatting dll
szjarvis 2016-04-08
  • 打赏
  • 举报
回复
HttpResponseMessage response = client.PostAsJsonAsync<Customer>(url, book).Result; 谁能解释一下这里的 book 是什么? 还有为什么我的client没有PostAsJsonAsync这玩意?
宝_爸 2015-11-18
  • 打赏
  • 举报
回复
我一般习惯于使用HttpWebRequest,可以这样 public virtual HttpWebRequest CreateWebRequest(string uri, NameValueCollection collHeader, string RequestMethod, bool NwCred) { HttpWebRequest webrequest = (HttpWebRequest) WebRequest.Create(uri); webrequest.KeepAlive = false; webrequest.Method = RequestMethod; int iCount = collHeader.Count; string key; string keyvalue; for (int i=0; i < iCount; i++) { key = collHeader.Keys[i]; keyvalue = collHeader[i]; webrequest.Headers.Add(key, keyvalue); } webrequest.ContentType = "text/html"; //"application/x-www-form-urlencoded"; if (ProxyServer.Length > 0) { webrequest.Proxy = new WebProxy(ProxyServer,ProxyPort); } webrequest.AllowAutoRedirect = false; if (NwCred) { CredentialCache wrCache = new CredentialCache(); wrCache.Add(new Uri(uri),"Basic", new NetworkCredential(UserName,UserPwd)); webrequest.Credentials = wrCache; } //Remove collection elements collHeader.Clear(); return webrequest; }//End of secure CreateWebRequest 代码来自 How to use HttpWebRequest and HttpWebResponse in .NET http://www.codeproject.com/Articles/6554/How-to-use-HttpWebRequest-and-HttpWebResponse-in-N
  • 打赏
  • 举报
回复

$("#button").click(function() {
                $.ajax({
                    type: "POST",
                    url: "http://192.168.0.124/testwebapi/api/AddUser",
                    beforeSend: function(request) {
                        request.setRequestHeader("ID", "123");
                        request.setRequestHeader("name", "张建");
                    },
                    success: function(result) {
                        alert(result);
                    }
                });
            });
wmy_03 2015-11-17
  • 打赏
  • 举报
回复
引用 2 楼 sgear 的回复:
忘记说了。不通过 JS形式。 不过上午已经解决了 HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Add("Account", "aaa"); client.DefaultRequestHeaders.Add("pwd", "bbb"); client.DefaultRequestHeaders.Add("rid ", "1"); HttpResponseMessage response = client.PostAsJsonAsync<Customer>(url, book).Result; 这种方式。不过现在还有个问题。就是 通过这种方式。。不知道怎么能拿到 返回值,返回值是一个 json格式的数据
你都有HttpResponseMessage,什么都能搞出来,
string json = response.Content.ReadAsStringAsync().Result;
一直学习 2015-11-17
  • 打赏
  • 举报
回复
忘记说了。不通过 JS形式。 不过上午已经解决了 HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Add("Account", "aaa"); client.DefaultRequestHeaders.Add("pwd", "bbb"); client.DefaultRequestHeaders.Add("rid ", "1"); HttpResponseMessage response = client.PostAsJsonAsync<Customer>(url, book).Result; 这种方式。不过现在还有个问题。就是 通过这种方式。。不知道怎么能拿到 返回值,返回值是一个 json格式的数据

110,565

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧