C#用httpclient模拟post到web网站上

客户端提供一个url,模拟服务端向这个urlpost数据(xml)流,客户端从这个url接受怎么实现啊... 客户端提供一个url,
模拟服务端 向这个url post数据(xml)流,
客户端从这个url接受

怎么实现啊
展开
 我来答
oldyanglong
推荐于2016-03-29
知道答主
回答量:32
采纳率:0%
帮助的人:9.2万
展开全部
实例化下面的类,设置相关属性(url,method)等,调用call就好

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using System.IO;
using System.Net;
using System.Xml;
namespace xxxu
{
public class XXXUHttpClient
{
//请求内容,无论post和get,都用get方式提供
private string reqContent;
//应答内容
private string resContent;
//请求方法
private string method;
//错误信息
private string errInfo;
//证书文件
private string certFile;
//证书密码
private string certPasswd;
//ca证书文件
private string caFile;
//超时时间,以秒为单位
private int timeOut;
//http应答编码
private int responseCode;
//字符编码
private string charset;
public HuizeHttpClient()
{
this.caFile = "";
this.certFile = "";
this.certPasswd = "";
this.reqContent = "";
this.resContent = "";
this.method = "POST";
this.errInfo = "";
this.timeOut = 1 * 60;//5分钟
this.responseCode = 0;
setCharset(System.Web.HttpContext.Current.Request.ContentEncoding.BodyName);
}
//设置请求内容
public void setReqContent(string reqContent)
{
this.reqContent = reqContent;
}
//获取结果内容
public string getResContent()
{
return this.resContent;
}
//设置请求方法post或者get
public void setMethod(string method)
{
this.method = method;
}
//获取错误信息
public string getErrInfo()
{
return this.errInfo;
}
//设置证书信息
public void setCertInfo(string certFile, string certPasswd)
{
this.certFile = certFile;
this.certPasswd = certPasswd;
}
//设置ca
public void setCaInfo(string caFile)
{
this.caFile = caFile;
}
//设置超时时间,以秒为单位
public void setTimeOut(int timeOut)
{
this.timeOut = timeOut;
}

//获取http状态码
public int getResponseCode()
{
return this.responseCode;
}
public void setCharset(string charset)
{
if (string.IsNullOrEmpty(charset))
{
this.charset = "UTF-8";
}
this.charset = charset;
}
//验证服务器证书
public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
return true;
}
//执行http调用
public bool call()
{
StreamReader sr = null;
HttpWebResponse wr = null;
HttpWebRequest hp = null;
try
{
string postData = null;
if (this.method.ToUpper() == "POST")
{
string[] sArray = System.Text.RegularExpressions.Regex.Split(this.reqContent, "\\?");
hp = (HttpWebRequest)WebRequest.Create(sArray[0]);
if (sArray.Length >= 2)
{
postData = sArray[1];
}
}
else
{
hp = (HttpWebRequest)WebRequest.Create(this.reqContent);
}

ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
if (this.certFile != "")
{
hp.ClientCertificates.Add(new X509Certificate2(this.certFile, this.certPasswd));
}
hp.Timeout = this.timeOut * 1000;
System.Text.Encoding encoding = System.Text.Encoding.GetEncoding(this.charset);
if (postData != null)
{
byte[] data = encoding.GetBytes(postData);
hp.Method = "POST";
hp.ContentType = "application/x-www-form-urlencoded";
hp.ContentLength = data.Length;
Stream ws = hp.GetRequestStream();
// 发送数据
ws.Write(data, 0, data.Length);
ws.Close();
}

wr = (HttpWebResponse)hp.GetResponse();
sr = new StreamReader(wr.GetResponseStream(), encoding);

this.resContent = sr.ReadToEnd();
sr.Close();
wr.Close();
}
catch (Exception exp)
{
this.errInfo += exp.Message;
if (wr != null)
{
this.responseCode = Convert.ToInt32(wr.StatusCode);
}
return false;
}
this.responseCode = Convert.ToInt32(wr.StatusCode);
return true;
}

}
}
更多追问追答
追问
为什么我发送之后的url 提示找不到服务器  难道服务端我只能发布之后才能测试?
这么说这个URL必须先存在 换句话说,我客户端的测试必须先写好发布是不是呢?
追答
是的,服务器的url必需存在
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式