首页 技术 正文
技术 2022年11月11日
0 收藏 495 点赞 2,855 浏览 4564 个字
摘自: http://geekswithblogs.net/rakker/archive/2006/04/21/76044.aspxHttp Post in C#
Searched out on the internet and didn't really find anything that was horribly succinct, so I wrote this class for fun. I had help from http://www.codeproject.com/cs/webservices/translation.asp. I hope you enjoy! Here's the code to call it:PostSubmitter post=new PostSubmitter();
post.Url="http://seeker.dice.com/jobsearch/servlet/JobSearch";
post.PostItems.Add("op","");
post.PostItems.Add("rel_code","");
post.PostItems.Add("FREE_TEXT","c# jobs");
post.PostItems.Add("SEARCH","");
post.Type=PostSubmitter.PostTypeEnum.Post;
string result=post.Post();And here's the class:using System;
using System.Text;
using System.IO;
using System.Web;
using System.Net;
using System.Collections.Specialized;namespace Snowball.Common
{
/// <summary>
/// Submits post data to a url.
/// </summary>
public class PostSubmitter
{
/// <summary>
/// determines what type of post to perform.
/// </summary>
public enum PostTypeEnum
{
/// <summary>
/// Does a get against the source.
/// </summary>
Get,
/// <summary>
/// Does a post against the source.
/// </summary>
Post
}private string m_url=string.Empty;
private NameValueCollection m_values=new NameValueCollection();
private PostTypeEnum m_type=PostTypeEnum.Get;
/// <summary>
/// Default constructor.
/// </summary>
public PostSubmitter()
{
}/// <summary>
/// Constructor that accepts a url as a parameter
/// </summary>
/// <param name="url">The url where the post will be submitted to.</param>
public PostSubmitter(string url):this()
{
m_url=url;
}/// <summary>
/// Constructor allowing the setting of the url and items to post.
/// </summary>
/// <param name="url">the url for the post.</param>
/// <param name="values">The values for the post.</param>
public PostSubmitter(string url, NameValueCollection values):this(url)
{
m_values=values;
}/// <summary>
/// Gets or sets the url to submit the post to.
/// </summary>
public string Url
{
get
{
return m_url;
}
set
{
m_url=value;
}
}
/// <summary>
/// Gets or sets the name value collection of items to post.
/// </summary>
public NameValueCollection PostItems
{
get
{
return m_values;
}
set
{
m_values=value;
}
}
/// <summary>
/// Gets or sets the type of action to perform against the url.
/// </summary>
public PostTypeEnum Type
{
get
{
return m_type;
}
set
{
m_type=value;
}
}
/// <summary>
/// Posts the supplied data to specified url.
/// </summary>
/// <returns>a string containing the result of the post.</returns>
public string Post()
{
StringBuilder parameters=new StringBuilder();
for (int i=;i < m_values.Count;i++)
{
EncodeAndAddItem(ref parameters,m_values.GetKey(i),m_values[i]);
}
string result=PostData(m_url,parameters.ToString());
return result;
}
/// <summary>
/// Posts the supplied data to specified url.
/// </summary>
/// <param name="url">The url to post to.</param>
/// <returns>a string containing the result of the post.</returns>
public string Post(string url)
{
m_url=url;
return this.Post();
}
/// <summary>
/// Posts the supplied data to specified url.
/// </summary>
/// <param name="url">The url to post to.</param>
/// <param name="values">The values to post.</param>
/// <returns>a string containing the result of the post.</returns>
public string Post(string url, NameValueCollection values)
{
m_values=values;
return this.Post(url);
}
/// <summary>
/// Posts data to a specified url. Note that this assumes that you have already url encoded the post data.
/// </summary>
/// <param name="postData">The data to post.</param>
/// <param name="url">the url to post to.</param>
/// <returns>Returns the result of the post.</returns>
private string PostData(string url, string postData)
{
HttpWebRequest request=null;
if (m_type==PostTypeEnum.Post)
{
Uri uri = new Uri(url);
request = (HttpWebRequest) WebRequest.Create(uri);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postData.Length;
using(Stream writeStream = request.GetRequestStream())
{
UTF8Encoding encoding = new UTF8Encoding();
byte[] bytes = encoding.GetBytes(postData);
writeStream.Write(bytes, , bytes.Length);
}
}
else
{
Uri uri = new Uri(url + "?" + postData);
request = (HttpWebRequest) WebRequest.Create(uri);
request.Method = "GET";
}
string result=string.Empty;
using (HttpWebResponse response = (HttpWebResponse) request.GetResponse())
{
using (Stream responseStream = response.GetResponseStream())
{
using (StreamReader readStream = new StreamReader (responseStream, Encoding.UTF8))
{
result = readStream.ReadToEnd();
}
}
}
return result;
}
/// <summary>
/// Encodes an item and ads it to the string.
/// </summary>
/// <param name="baseRequest">The previously encoded data.</param>
/// <param name="dataItem">The data to encode.</param>
/// <returns>A string containing the old data and the previously encoded data.</returns>
private void EncodeAndAddItem(ref StringBuilder baseRequest, string key, string dataItem)
{
if (baseRequest==null)
{
baseRequest=new StringBuilder();
}
if (baseRequest.Length!=)
{
baseRequest.Append("&");
}
baseRequest.Append(key);
baseRequest.Append("=");
baseRequest.Append(System.Web.HttpUtility.UrlEncode(dataItem));
}
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,110
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,584
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,431
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,202
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,837
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,920