首页 > 代码库 > c# post basic 接口
c# post basic 接口
string url = "http://xxxxxxxxx";//地址,请反复检查地址的准确性
string usernamePassword = "username:password";
byte[] bytes = Encoding.Default.GetBytes(usernamePassword);
string str = Convert.ToBase64String(bytes);
string ret = new RequestHelper().HttpBasicPost(url, unionid, str);
public class RequestHelper { /// <summary> /// 设置cookie /// </summary> private CookieContainer cookie; public CookieContainer Cookie { get { return cookie; } set { cookie = value; } } /// <summary> /// 是否允许重定向 /// </summary> private bool allowAutoRedirect = true; /// <summary> /// 是否允许重定向(默认:true) /// </summary> public bool AllowAutoRedirect { get { return allowAutoRedirect; } set { allowAutoRedirect = value; } } /// <summary> /// contentType /// </summary> private string contentType = "application/x-www-form-urlencoded"; /// <summary> /// 设置contentType(默认:application/x-www-form-urlencoded) /// </summary> public string ContentType { get { return contentType; } set { contentType = value; } } /// <summary> /// accept /// </summary> private string accept = "*/*"; /// <summary> /// 设置accept(默认:*/*) /// </summary> public string Accept { get { return accept; } set { accept = value; } } /// <summary> /// 过期时间(默认:30000) /// </summary> private int timeOut = 30000; public int TimeOut { get { return timeOut; } set { timeOut = value; } } private string userAgent; /// <summary> /// /// </summary> public string UserAgent { get { return userAgent; } set { userAgent = value; } } private string acceptLanguage; /// <summary> /// 接受的语言 /// </summary> public string AcceptLanguage { get { return acceptLanguage; } set { acceptLanguage = value; } } /// <summary> /// 处理POST请求 /// </summary> /// <param name="url"></param> /// <param name="postdata"></param> /// <returns></returns> public string HttpPost(string url, string postdata) { var request = CreateWebRequest(url); request.Method = "POST"; if (!string.IsNullOrWhiteSpace(postdata)) { var bytesToPost = Encoding.UTF8.GetBytes(postdata); request.ContentLength = bytesToPost.Length; using (Stream requestStream = request.GetRequestStream()) { requestStream.Write(bytesToPost, 0, bytesToPost.Length); requestStream.Close(); } } using (var response = (HttpWebResponse)request.GetResponse()) { using (var sr = new StreamReader(response.GetResponseStream())) { var result = sr.ReadToEnd(); return result; } } } public string HttpBasicPost(string url, string postdata, string basic) { var request = CreateWebRequest(url); request.Method = "POST"; request.Headers.Add("Authorization", "Basic " + basic); if (!string.IsNullOrWhiteSpace(postdata)) { var bytesToPost = Encoding.UTF8.GetBytes(postdata); request.ContentLength = bytesToPost.Length; using (Stream requestStream = request.GetRequestStream()) { requestStream.Write(bytesToPost, 0, bytesToPost.Length); requestStream.Close(); } } using (var response = (HttpWebResponse)request.GetResponse()) { using (var sr = new StreamReader(response.GetResponseStream())) { var result = sr.ReadToEnd(); return result; } } } /// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="url"></param> /// <param name="obj"></param> /// <returns></returns> public T HttpPost<T>(string url, object obj) { var resultStr = HttpPost(url, JsonConvert.SerializeObject(obj)); return JsonConvert.DeserializeObject<T>(resultStr); } public T HttpPost<T>(string url, object obj, out string result, Func<string, string> serializeStrFunc = null) { var str = JsonConvert.SerializeObject(obj); if (serializeStrFunc != null) { str = serializeStrFunc(str); } result = HttpPost(url, str); return JsonConvert.DeserializeObject<T>(result); } /// <summary> /// post请求返回html /// </summary> /// <param name="url"></param> /// <param name="postDataStr"></param> /// <returns></returns> public string HttpPost(string url, Dictionary<string, string> postdata) { string postDataStr = null; if (postdata != null && postdata.Count > 0) { postDataStr = string.Join("&", postdata.Select(it => it.Key + "=" + it.Value)); } return HttpPost(url, postDataStr); } /// <summary> /// get请求获取返回的html /// </summary> /// <param name="url">无参URL</param> /// <param name="querydata">参数</param> /// <returns></returns> public string HttpGet(string url, Dictionary<string, string> querydata) { if (querydata != null && querydata.Count > 0) { url += "?" + string.Join("&", querydata.Select(it => it.Key + "=" + it.Value)); } return HttpGet(url); } /// <summary> /// get请求获取返回的html /// </summary> /// <param name="url"></param> /// <returns></returns> public string HttpGet(string url) { HttpWebRequest request = CreateWebRequest(url); request.Method = "GET"; // response.Cookies = cookie.GetCookies(response.ResponseUri); using (var response = (HttpWebResponse)request.GetResponse()) { using (var sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8"))) { var result = sr.ReadToEnd(); return result; } } } protected HttpWebRequest CreateWebRequest(string url) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); //request.ContentType = "text/html;charset=UTF-8"; request.ContentType = this.ContentType; if (cookie != null) request.CookieContainer = this.Cookie; if (string.IsNullOrEmpty(this.AcceptLanguage)) { WebHeaderCollection myWebHeaderCollection = request.Headers; myWebHeaderCollection.Add("Accept-Language", this.AcceptLanguage); } request.Accept = this.Accept; request.UseDefaultCredentials = true; request.UserAgent = this.UserAgent; request.Timeout = this.TimeOut; request.AllowAutoRedirect = this.AllowAutoRedirect; this.SetCertificatePolicy(); return request; } /// <summary> /// POST文件 /// </summary> /// <param name="url"></param> /// <param name="file">文件路径</param> /// <param name="postdata"></param> /// <returns></returns> public string HttpUploadFile(string url, string file, Dictionary<string, string> postdata) { return HttpUploadFile(url, file, postdata, Encoding.UTF8); } /// <summary> /// POST文件 /// </summary> /// <param name="url"></param> /// <param name="file">文件路径</param> /// <param name="postdata">参数</param> /// <param name="encoding"></param> /// <returns></returns> public string HttpUploadFile(string url, string file, Dictionary<string, string> postdata, Encoding encoding) { return HttpUploadFile(url, new string[] { file }, postdata, encoding); } /// <summary> /// POST文件 /// </summary> /// <param name="url"></param> /// <param name="files">文件路径</param> /// <param name="postdata">参数</param> /// <returns></returns> public string HttpUploadFile(string url, string[] files, Dictionary<string, string> postdata) { return HttpUploadFile(url, files, postdata, Encoding.UTF8); } /// <summary> /// POST文件 /// </summary> /// <param name="url"></param> /// <param name="files">文件路径</param> /// <param name="postdata">参数</param> /// <param name="encoding"></param> /// <returns></returns> public string HttpUploadFile(string url, string[] files, Dictionary<string, string> postdata, Encoding encoding) { string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x"); byte[] boundarybytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n"); byte[] endbytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n"); HttpWebRequest request = CreateWebRequest(url); request.ContentType = "multipart/form-data; boundary=" + boundary; request.Method = "POST"; request.KeepAlive = true; ; request.AllowAutoRedirect = this.AllowAutoRedirect; if (this.Cookie != null) request.CookieContainer = this.Cookie; request.Credentials = CredentialCache.DefaultCredentials; using (Stream stream = request.GetRequestStream()) { //1.1 key/value string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}"; if (postdata != null) { foreach (string key in postdata.Keys) { stream.Write(boundarybytes, 0, boundarybytes.Length); string formitem = string.Format(formdataTemplate, key, postdata[key]); byte[] formitembytes = encoding.GetBytes(formitem); stream.Write(formitembytes, 0, formitembytes.Length); } } //1.2 file string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: application/octet-stream\r\n\r\n"; byte[] buffer = new byte[4096]; int bytesRead = 0; for (int i = 0; i < files.Length; i++) { stream.Write(boundarybytes, 0, boundarybytes.Length); string header = string.Format(headerTemplate, "file" + i, Path.GetFileName(files[i])); byte[] headerbytes = encoding.GetBytes(header); stream.Write(headerbytes, 0, headerbytes.Length); using (FileStream fileStream = new FileStream(files[i], FileMode.Open, FileAccess.Read)) { while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) { stream.Write(buffer, 0, bytesRead); } } } //1.3 form end stream.Write(endbytes, 0, endbytes.Length); } //2.WebResponse HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (StreamReader stream = new StreamReader(response.GetResponseStream())) { return stream.ReadToEnd(); } } /// <summary> /// 获得响应中的图像 /// </summary> /// <param name="url"></param> /// <returns></returns> public Stream GetResponseImage(string url) { Stream stream = null; try { HttpWebRequest request = CreateWebRequest(url); request.KeepAlive = true; request.Method = "GET"; HttpWebResponse res = (HttpWebResponse)request.GetResponse(); stream = res.GetResponseStream(); return stream; } catch { return null; } } /// <summary> /// 正则获取匹配的第一个值 /// </summary> /// <param name="html"></param> /// <param name="pattern"></param> /// <returns></returns> private string GetStringByRegex(string html, string pattern) { Regex re = new Regex(pattern, RegexOptions.IgnoreCase); MatchCollection matchs = re.Matches(html); if (matchs.Count > 0) { return matchs[0].Groups[1].Value; } else return ""; } /// <summary> /// 正则验证返回的response是否正确 /// </summary> /// <param name="html">Html内容</param> /// <param name="pattern">正则表达式</param> /// <returns></returns> private bool VerifyResponseHtml(string html, string pattern) { Regex re = new Regex(pattern); return re.IsMatch(html); } //注册证书验证回调事件,在请求之前注册 private void SetCertificatePolicy() { ServicePointManager.ServerCertificateValidationCallback += RemoteCertificateValidate; } /// <summary> /// 远程证书验证,固定返回true /// </summary> private static bool RemoteCertificateValidate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error) { return true; } #region 提交方式 /// <summary> /// 提交Https /// Post /// </summary> /// <param name="url">URL地址</param> /// <param name="parsStr">参数</param> public static string SubmitHttpsPost(string url, string parsStr) { Encoding encode = System.Text.Encoding.UTF8; byte[] arrB = encode.GetBytes(parsStr); HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url); myReq.Method = "POST"; myReq.ContentType = "application/x-www-form-urlencoded"; myReq.ContentLength = arrB.Length; Stream outStream = myReq.GetRequestStream(); outStream.Write(arrB, 0, arrB.Length); outStream.Close(); //接收HTTP做出的响应 WebResponse myResp = myReq.GetResponse(); Stream ReceiveStream = myResp.GetResponseStream(); StreamReader readStream = new StreamReader(ReceiveStream, encode); Char[] read = new Char[256]; int count = readStream.Read(read, 0, 256); string str = null; while (count > 0) { str += new String(read, 0, count); count = readStream.Read(read, 0, 256); } readStream.Close(); myResp.Close(); if (!string.IsNullOrEmpty(str)) { return str; } else { return "无返回值"; } } #endregion }
basic请求的两点:
1、将用户名和密码进行编码
2、设置请求头request.Headers.Add("Authorization", "Basic " + basic);
post请求注意点:
要设置request.ContentLength,以及将表单内容写入请求中,不然会出现411长度错误
如果都尝试了还是404,请再次检查地址的准确性,在上面坑了一天!!!!!!!!!
c# post basic 接口
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。