asp.net 调用post方法并获取返回值
2014-02-28 11:45
681 查看
/// <summary>
/// http协议 post数据 接受返回结果
/// </summary>
/// <param name="url"></param>
/// <param name="paramName"></param>
/// <param name="paramValue"></param>
/// <returns></returns>
protected string PostRequest(string url, List<string> paramName, List<string> paramValue)
{
string result = string.Empty;
try
{
if (paramName != null && paramValue != null)
{
// 编辑并Encoding提交的数据
StringBuilder sbuilder = new StringBuilder(paramName[0] + "=" + paramValue[0]);
for (int i = 1; i < paramName.Count; i++)
sbuilder.Append("&" + paramName[i] + "=" + paramValue[i]);
//byte[] data = Encoding.Default.GetBytes(sbuilder.ToString());
byte[] data = Encoding.UTF8.GetBytes(sbuilder.ToString());
// 发送请求
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
using (Stream stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
stream.Close();
}
// 获得回复
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
result = reader.ReadToEnd();
reader.Close();
response.Close();
}
}
}
catch (Exception ex)
{
Log.Fatal("异常!"+ex.Message);//记录错误log
if (ex == null)
ex = null;
}
return result;
}
/// http协议 post数据 接受返回结果
/// </summary>
/// <param name="url"></param>
/// <param name="paramName"></param>
/// <param name="paramValue"></param>
/// <returns></returns>
protected string PostRequest(string url, List<string> paramName, List<string> paramValue)
{
string result = string.Empty;
try
{
if (paramName != null && paramValue != null)
{
// 编辑并Encoding提交的数据
StringBuilder sbuilder = new StringBuilder(paramName[0] + "=" + paramValue[0]);
for (int i = 1; i < paramName.Count; i++)
sbuilder.Append("&" + paramName[i] + "=" + paramValue[i]);
//byte[] data = Encoding.Default.GetBytes(sbuilder.ToString());
byte[] data = Encoding.UTF8.GetBytes(sbuilder.ToString());
// 发送请求
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
using (Stream stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
stream.Close();
}
// 获得回复
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
result = reader.ReadToEnd();
reader.Close();
response.Close();
}
}
}
catch (Exception ex)
{
Log.Fatal("异常!"+ex.Message);//记录错误log
if (ex == null)
ex = null;
}
return result;
}
相关文章推荐
- 【转】asp.net 项目在 IE 11 下出现 “__doPostBack”未定义 的解决办法
- asp.net的母版页使用
- ASP.NET中树型DropDownList的绑定
- ASP.NET缓存 Cache之数据缓存
- asp.net发布到IIS中出现错误:处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”
- asp.net发布到IIS中出现错误:处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”
- Asp.net中RegisterStartupScript方法的使用
- ASP.NET购物车(源码下载)
- ASP.Net Controls 用法大全
- ASP.NET 因为无法创建应用程序域,因此未能执行请求。错误号:0x80070002
- ASP.NET MVC Ninject 实现依赖注入
- asp.net程序保护的思路
- asp.net如何判断网址能否打开
- ASP.NET中的TextBox下划线
- ASP.NET批量删除数据
- 解决 Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects
- ASP.NET MOBILE开发体会
- ASP.NET(C#) 读取EXCEL问题汇总
- [Asp.net]常见word,excel,ppt,pdf在线预览方案,有图有真相,总有一款适合你!
- asp.net读取CSV