Http.CS
public class Http
{
public static string Post(string requestUri,string json)
{
string strUri = requestUri;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strUri);
request.Method = "POST";
request.ContentType = "application/json";
string paraUriCoded = json;
byte[] payload;
payload = System.Text.Encoding.UTF8.GetBytes(paraUriCoded);
request.ContentLength = payload.Length;
Stream writer;
try
{
writer = request.GetRequestStream();
}
catch (Exception ex)
{
return ex.ToString();
}
writer.Write(payload, 0, payload.Length);
writer.Close();
HttpWebResponse response;
try
{
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
response = ex.Response as HttpWebResponse;
}
Stream s = response.GetResponseStream();
StreamReader sRead = new StreamReader(s);
string postContent = sRead.ReadToEnd().ToString();
sRead.Close();
return postContent;
}
}
调用方法
Http.Post(api连接, 需要发送的json数据);
示例
string url = "http://***.*****.***";
string ceshi = "{\r\n \"data\": {" + "},\r\n \"timestamp\": "+ timeS + ",\r\n \"sign\": \"\"\r\n}";
string yu = Http.Post(url, ceshi);
免责声明
本学习笔记旨在提供编程语言和相关主题的信息和资源,帮助读者学习和掌握编程技能。然而,请注意以下事项:
准确性和完整性:尽管本博客内容经过精心编写和研究,但作者无法保证信息始终准确、完整或最新。编程语言和技术领域不断发展,读者应自行核实和查阅官方文档以确保信息的最新和准确。
文章评论