调用webapi的方法

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

请求方式:Post

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

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

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

 

name string 名称

 

 

$("#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);
                    }
                });
            });

这个是后台部分

 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;
string json = response.Content.ReadAsStringAsync().Result;

***分隔符***
 

我一般习惯于使用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

 

 

 

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值