你如何在C#中进行远程http发布(请求)?解决方法 这是我写过的一个小应用程序的代码,用于将带有值的表单发布到URL.它应该非常强大.
_formValues是一个字典< string,string>包含要发布的变量及其值.
// encode form dataStringBuilder poststring = new StringBuilder();bool first=true;foreach (keyvaluePair pair in _formValues){ if(first) first=false; else poststring.Append("&"); poststring.AppendFormat("{0}={1}",pair.Key,System.Web.httpUtility.UrlEncode(pair.Value));}ASCIIEnCoding ascii = new ASCIIEnCoding();byte[] postBytes = ascii.GetBytes(poststring.ToString());// set up request objecthttpWebRequest request;try{ request = WebRequest.Create(url) as httpWebRequest;}catch (UriFormatException){ request = null;}if (request == null) throw new ApplicationException("InvalID URL: " + url);request.Method = "POST";request.ContentType = "application/x-www-form-urlencoded";request.ContentLength = postBytes.Length;// add post data to requestStream poststream = request.GetRequestStream();poststream.Write(postBytes,postBytes.Length);poststream.Close();httpWebResponse response = request.GetResponse() as httpWebResponse;总结
以上是内存溢出为你收集整理的使用C#远程HTTP发布全部内容,希望文章能够帮你解决使用C#远程HTTP发布所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)