Let's go

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("同步失败!");
    }
}

 

posted @ 2019-10-29 09:28  chenze  阅读(339)  评论(0编辑  收藏  举报
有事您Q我