无网不进  
软硬件开发
  1 using System;
  2 using System.IO;
  3 using System.Net;
  4 using System.Text;
  5 using DevExpress.Xpo;
  6 
  7 namespace CSharpDemo
  8 {
  9 
 10     public class HttpAPI
 11     {
 12 
 13         private static string BaseUri;
 14 
 15         public HttpAPI(string baseUri)
 16         {
 17             BaseUri = baseUri;
 18         }
 19 
 20 
 21         #region Delete方式
 22         private static string Delete(string uri, string data = "")
 23         {
 24             string serviceUrl = "";
 25             if (BaseUri == "" || BaseUri == null)
 26             {
 27                 serviceUrl = uri;
 28             }
 29             else
 30             {
 31                 serviceUrl = string.Format("{0}/{1}", BaseUri, uri);
 32             }
 33             return CommonHttpRequest(serviceUrl, "DELETE", data);
 34         }
 35         #endregion
 36 
 37 
 38 
 39         #region Put方式
 40         private static string Put(string uri, string data)
 41         {
 42             string serviceUrl = "";
 43             if (BaseUri == "" || BaseUri == null)
 44             {
 45                 serviceUrl = uri;
 46             }
 47             else
 48             {
 49                 serviceUrl = string.Format("{0}/{1}", BaseUri, uri);
 50             }
 51             return CommonHttpRequest(serviceUrl, "PUT", data);
 52         }
 53         #endregion
 54 
 55 
 56 
 57 
 58         #region POST方式实现
 59         private static string Post(string uri, string data)
 60         {
 61             string serviceUrl = "";
 62             if (BaseUri == "" || BaseUri == null)
 63             {
 64                 serviceUrl = uri;
 65             }
 66             else
 67             {
 68                 serviceUrl = string.Format("{0}/{1}", BaseUri, uri);
 69             }
 70             return CommonHttpRequest(serviceUrl, "Post", data);
 71         }
 72         #endregion
 73 
 74 
 75 
 76         #region GET方式实现
 77         private static string Get(string uri, string data)
 78         {
 79             string serviceUrl = "";
 80             if (BaseUri == "" || BaseUri == null)
 81             {
 82                 serviceUrl = uri;
 83             }
 84             else
 85             {
 86                 serviceUrl = string.Format("{0}/{1}", BaseUri, uri);
 87             }
 88             return CommonHttpRequest(serviceUrl, "GET", data);
 89         }
 90         #endregion
 91 
 92 
 93 
 94         #region  私有方法
 95         private static string CommonHttpRequest(string url, string type, string data)
 96         {
 97             HttpWebRequest myRequest = null;
 98             Stream outstream = null;
 99             HttpWebResponse myResponse = null;
100             StreamReader reader = null;
101             try
102             {
103                 //构造http请求的对象
104                 myRequest = (HttpWebRequest)WebRequest.Create(url);
105                 //设置
106                 myRequest.ProtocolVersion = HttpVersion.Version10;
107                 myRequest.Method = type;
108                 myRequest.ContentType = "application/json ;charset = UTF-8";
109                 myRequest.Timeout = 5000;
110                 myRequest.Proxy = null;
111                 if (data.Trim() != "")
112                 {
113                     //myRequest.ContentType = "text/xml";
114                     //myRequest.ContentType = "application/x-www-form-urlencoded";
115                     myRequest.ContentType = "application/json ;charset = UTF-8";
116                     //myRequest.Timeout = 5000;
117                     myRequest.Proxy = null;
118                     //myRequest.ContentLength = data.Length;     //当格式修改时,导致数据长度错误 
119                     //StreamWriter writer = new StreamWriter(myRequest.GetRequestStream(), Encoding.ASCII);
120                     StreamWriter writer = new StreamWriter(myRequest.GetRequestStream(), Encoding.GetEncoding("UTF-8"));
121                     writer.Write(data);
122                     writer.Flush();
123                     /*
124                     //myRequest.Headers.Add("data", data);
125                     //转成网络流
126                     byte[] buf = Encoding.GetEncoding("UTF-8").GetBytes(data);
127                     outstream = myRequest.GetRequestStream();
128                     outstream.Flush();
129                     outstream.Write(buf, 0, buf.Length);
130                     outstream.Flush();
131                     outstream.Close();
132                     */
133                 }
134                 // 获得接口返回值
135                 myResponse = (HttpWebResponse)myRequest.GetResponse();
136                 reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
137                 string ReturnXml = reader.ReadToEnd();
138                 reader.Close();
139                 myResponse.Close();
140                 myRequest.Abort();
141                 return ReturnXml;
142             }
143             catch (Exception)
144             {
145                 // throw new Exception();
146                 if (outstream != null)
147                     outstream.Close();
148                 if (reader != null)
149                     reader.Close();
150                 if (myResponse != null)
151                     myResponse.Close();
152                 if (myRequest != null)
153                     myRequest.Abort();
154                 return "";
155             }
156         }
157         #endregion
158 
159 
160         #region 通用请求
161         /// <summary>
162         /// Http通用请求
163         /// </summary>
164         /// <param name="url"></param>
165         /// <param name="type"></param>
166         /// <param name="inputData"></param>
167         /// <returns></returns>
168         public static string HttpRequest(string url, HttpType type, string inputData)
169         {
170             switch (type)
171             {
172                 case HttpType.PUT:
173                     return Put(url, inputData);
174                 case HttpType.GET:
175                     return Get(url,inputData);
176                 case HttpType.POST:
177                     return Post(url, inputData);
178                 case HttpType.DELETE:
179                     return Delete(url, inputData);
180                 default:
181                     break;
182             }
183             return "";
184         }
185 
186 
187 
188         /// <summary>
189         /// Http通用请求
190         /// </summary>
191         /// <param name="ip"></param>
192         /// <param name="port"></param>
193         /// <param name="uri"></param>
194         /// <param name="type"></param>
195         /// <param name="inputData"></param>
196         /// <returns></returns>
197         public static string HttpRequest(string ip, string port, string uri, HttpType type, string inputData)
198         {
199             string url = "http://" + ip + ":" + port + uri;
200             return HttpRequest(url, type, inputData);
201         }
202 
203         #endregion
204         public enum HttpType
205         {
206 
207             PUT = 0,
208             GET = 1,
209             POST = 2,
210             DELETE = 3
211 
212         }
213     }
214 
215 }

 

参考路径: https://blog.csdn.net/shu19880720/article/details/78424635

因为对接海康的某协议用到了http,本来已以往的经验使用http会很简单,但post和get一直提交xml数据失败,几乎试遍了网上常用的方式还是没有效果,三天后还是团队有人用socket直接发送到成功了,不甘于直接用socket,继续研究终于发现header里也要存放数据才可以 ,一句 myRequest.Headers.Add("data", data);让我三天半浑身不爽,记录下,以免后边继续掉坑里

使用的类库整理如下:

 

posted on 2018-08-17 17:22  无网不进  阅读(1263)  评论(0)    收藏  举报