C#请求接口

上代码,方便查看【摘抄】

System.Net.WebClient WebClientObj = new System.Net.WebClient();
System.Collections.Specialized.NameValueCollection PostVars = new System.Collections.Specialized.NameValueCollection();
PostVars.Add("userid", userid);


byte[] byRemoteInfo = null;
string url = "http://~";
byRemoteInfo = WebClientObj.UploadValues(url, "POST", PostVars);
string str = System.Text.Encoding.UTF8.GetString(byRemoteInfo);

 使用案例:

protected void ebtSync_Click(object sender, EventArgs e)
{
    DataTable dtChoose = GridShow1.ChooseTable;
    System.Net.WebClient WebClientObj = new System.Net.WebClient();
    System.Collections.Specialized.NameValueCollection PostVars = new System.Collections.Specialized.NameValueCollection();
    PostVars.Add("usercode", "aa");
    PostVars.Add("module", "bb");
    byte[] byRemoteInfo = null;
    string url = "http://localhost:8080/DaiBan.ashx";
    byRemoteInfo = WebClientObj.UploadValues(url, "POST", PostVars);
    string str = System.Text.Encoding.UTF8.GetString(byRemoteInfo);
    testEntity result = new JavaScriptSerializer().Deserialize<testEntity>(str);
    if (str == "success")
    {
        this.Message("同步成功!");
    }
    else {
        this.Message("同步失败!");
    }
}
作者:chenze
出处:https://www.cnblogs.com/chenze-Index/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
如果文中有什么错误,欢迎指出。以免更多的人被误导。
原文地址:https://www.cnblogs.com/chenze-Index/p/11756882.html