关于C# WinForm调用HTTP接口问题
现在有一个HTTP接口,这个接口用Postman测试结果如图所示现在要在C#WinForm中调用这个接口,因为以前完全没有这方面经验,所以在网上找了段代码(字数超出,把T...
现在有一个HTTP接口,这个接口用Postman测试结果如图所示
现在要在C# WinForm中调用这个接口,因为以前完全没有这方面经验,所以在网上找了段代码 (字数超出,把Try部分去掉了)
public bool getweb(string strURL, out string buf, string postData)
{
buf = "";
CookieContainer cc = new CookieContainer();
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(postData);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
Stream newStream = request.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
request.CookieContainer = cc;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
cc.Add(response.Cookies);
Stream stream = response.GetResponseStream();
string sHtml = new StreamReader(stream, System.Text.Encoding.Default).ReadToEnd();
buf = sHtml;
return true;
}
}
我是这样调用的,老是提示 {"StatusCode":2,"Message":"data cannot be empty.","Result":null}
string strUrl="http://210.3.215.78//MODUser/SelectClinic";
string strBuf = string.Empty;
string strPostData="clinic_code=001";
getweb(strUrl, out strBuf, strPostData);
MessageBox.Show(strBuf);
请教下大侠怎样调用,谢谢 展开
现在要在C# WinForm中调用这个接口,因为以前完全没有这方面经验,所以在网上找了段代码 (字数超出,把Try部分去掉了)
public bool getweb(string strURL, out string buf, string postData)
{
buf = "";
CookieContainer cc = new CookieContainer();
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(postData);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
Stream newStream = request.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
request.CookieContainer = cc;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
cc.Add(response.Cookies);
Stream stream = response.GetResponseStream();
string sHtml = new StreamReader(stream, System.Text.Encoding.Default).ReadToEnd();
buf = sHtml;
return true;
}
}
我是这样调用的,老是提示 {"StatusCode":2,"Message":"data cannot be empty.","Result":null}
string strUrl="http://210.3.215.78//MODUser/SelectClinic";
string strBuf = string.Empty;
string strPostData="clinic_code=001";
getweb(strUrl, out strBuf, strPostData);
MessageBox.Show(strBuf);
请教下大侠怎样调用,谢谢 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询