C# 通过接口 post 请求
来源:互联网 发布:js移动div位置 编辑:程序博客网 时间:2024/04/20 19:29
/// <summary> /// 提交数据请求 方法一 /// </summary> /// <param name="POSTURL">请求提交的地址 如:http://xxx.xxx.xxx/interface/TestPostRequest</param> /// <param name="PostData">提交的数据(字符串)</param> /// <returns></returns> public static string RequestData(string POSTURL, string PostData) { //发送请求的数据 WebRequest myHttpWebRequest = WebRequest.Create(POSTURL); myHttpWebRequest.Method = "POST"; UTF8Encoding encoding = new UTF8Encoding(); byte[] byte1 = encoding.GetBytes(PostData); myHttpWebRequest.ContentType = "application/x-www-form-urlencoded"; myHttpWebRequest.ContentLength = byte1.Length; Stream newStream = myHttpWebRequest.GetRequestStream(); newStream.Write(byte1, 0, byte1.Length); newStream.Close(); //发送成功后接收返回的XML信息 HttpWebResponse response = (HttpWebResponse)myHttpWebRequest.GetResponse(); string lcHtml = string.Empty; Encoding enc = Encoding.GetEncoding("UTF-8"); Stream stream = response.GetResponseStream(); StreamReader streamReader = new StreamReader(stream, enc); lcHtml = streamReader.ReadToEnd(); return lcHtml; }
/// <summary> /// 通过接口提交请求的公用方法 方法二 /// </summary> /// <param name="url">请求的接口地址 如:http://xxx.xxx.xxx/interface/TestPostRequest</param> /// <param name="senddata">要发送的报文</param> /// <returns></returns> public string GetPostStr(string url, string senddata) { System.Net.WebClient webc = new System.Net.WebClient(); var apiurl = new Uri(url); string sendstr = senddata; webc.Headers.Add("Content-Type", "text/xml"); //webc.Headers["Content-Type"] = "application/stream;charset=utf-8";//OK var arr = webc.UploadData(apiurl, Encoding.UTF8.GetBytes(sendstr)); return Encoding.UTF8.GetString(arr) ; }
/// <summary> /// 服务端接收请求、处理并返回响应的数据 /// </summary> public void TestPostRequest() { StreamReader reader = new StreamReader(System.Web.HttpContext.Current.Request.InputStream); String xmlData = reader.ReadToEnd(); //string strRequestData = "接收到的请求数据是:" + xmlData; //string strReponseData = "要返回的数据是:" + "<root><attReaderName>1</attReaderName></root>"; string strReponseData= ""; strReponseData+= "<root>" + "<head>" + "<errorCode>0</errorCode>" + "<result>success</result>" + "</head>" + "</root>"; HttpContext.Current.Response.Write(strReponseData); }
0 0
- C# 通过接口 post 请求
- c# 通过post请求
- C#通过发送 http 请求调用接口之Post
- C# 通过GET、POST请求获取结果
- loadrunner通过web的post请求方法测接口
- loadrunner通过web的post请求方法测接口 1
- loadrunner通过web的post请求方法测接口 2
- loadrunner通过web的post请求方法测接口 1
- java post请求接口
- post请求https接口
- POST请求接口
- C#与PHP接口交互,通过Post方式
- C# http post请求
- C#模拟post请求
- C#的post请求
- C# Post请求 方法
- C# 发送POST请求
- C# post 请求
- 关于hibernate持久化所造成的问题以及解决方案
- Mysql拆库两种重命名schema名的方法及其性能
- mac下解决mysql不兼容中文问题
- 【opencv3.0】打开摄像头
- git命令
- C# 通过接口 post 请求
- UICollectionView的使用
- IOS触摸事件处理(如何处理多个触摸点)
- Ubuntu添加开机自动启动程序的方法
- CentOS 6 64位的U盘安装方法
- The operation couldn't be completed.(LaunchServicesError error 0.)问题描述和解决方法
- Python2.7下安装matplotlib-2维图形库(windows系统)
- 在OpenGL ES中使用glDrawElements时的注意事项
- 【笔记】右键添加CMD方法