.net post提交

接口调用:post提交获取返回值

String SUPPLIER_ADD_REQ = "Request=";//键
String SUPPLIER_REQ_URL = https://xxxxxxx/query";//方法链接
String createDesXML = "sjfslfajfsafsjj";//值
String result = null;

//获取接口返回结果
result =SendPost(SUPPLIER_ADD_REQ + createDesXML, SUPPLIER_REQ_URL);

public static string SendPost(string formData, string formUrl)
{
try
{

//注意提交的编码 这边是需要改变的 这边默认的是Default:系统当前编码

byte[] postData = Encoding.UTF8.GetBytes(formData);

// 设置提交的相关参数
HttpWebRequest request = WebRequest.Create(formUrl) as HttpWebRequest;
Encoding myEncoding = Encoding.UTF8;
request.Method = "POST";
request.KeepAlive = false;
request.AllowAutoRedirect = true;
request.ContentType = "application/x-www-form-urlencoded";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
request.ContentLength = postData.Length;

// 提交请求数据
System.IO.Stream outputStream = request.GetRequestStream();
outputStream.Write(postData, 0, postData.Length);
outputStream.Close();

 

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string result = reader.ReadToEnd();
reader.Close();
return result;
}
catch (Exception ex)
{
return "error";
}
}

总结:这是我post提交接口返回数据的方法

posted on 2018-06-21 18:23  Smile向前  阅读(299)  评论(0编辑  收藏  举报