asp.net中WebRequest Post 及Get 数据实例

sqlserver/42852.htm target=_blank >

 代码如下 复制代码

 

public class WebRequestHelper
     {
         /// <summary>
         /// 以POST 形式请求数据
         /// </summary>
         /// <param name="RequestPara"></param>
         /// <param name="Url"></param>
         /// <returns></returns>
       public  static string PostData(string RequestPara,string Url)
         {
             WebRequest hr = HttpWebRequest.Create(Url);
 
             byte[] buf = System.Text.Encoding.GetEncoding("utf-8").GetBytes(RequestPara);
             hr.ContentType = "application/x-www-form-urlencoded";
             hr.ContentLength = buf.Length;
             hr.Method = "POST";
 
             System.IO.Stream RequestStream = hr.GetRequestStream();
             RequestStream.Write(buf, 0, buf.Length);
             RequestStream.Close();
 
             System.Net.WebResponse response = hr.GetResponse();
             StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8"));
             string ReturnVal = reader.ReadToEnd();
             reader.Close();           
             response.Close();
 
             return ReturnVal;
         }
 
         /// <summary>
         /// 以GET 形式获取数据
         /// </summary>
         /// <param name="RequestPara"></param>
         /// <param name="Url"></param>
         /// <returns></returns>
 
       public static  string GetData(string RequestPara, string Url)
         {
             RequestPara=RequestPara.IndexOf('?')>-1?(RequestPara):("?"+RequestPara);
 
             WebRequest hr = HttpWebRequest.Create(Url + RequestPara);
 
             byte[] buf = System.Text.Encoding.GetEncoding("utf-8").GetBytes(RequestPara);         
             hr.Method = "GET";
 
             System.Net.WebResponse response = hr.GetResponse();
             StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8"));
             string ReturnVal = reader.ReadToEnd();
             reader.Close();
             response.Close();
 
             return ReturnVal;
         }
     }

时间: 2024-07-31 08:03:09

asp.net中WebRequest Post 及Get 数据实例的相关文章

asp.net中使用sqlite无法插入数据

问题描述 asp.net中使用sqlite无法插入数据 提示SQL logic error or missing database no such table: users 解决方案 Asp.net使用Sqlite数据库[学习笔记][asp.net下SQL语句无法向ACCESS数据库插入数据的小提示asp.net使用DataSet数据集插入记录 解决方案二: 没有users这个表,你的sql语句是不是有问题 解决方案三: sql语句没有问题,现在已经解决了,是web.config中的配置有问题,

ASP.NET中 Execl导出的六种方法实例

 这篇文章主要介绍了ASP.NET中 Execl导出的六种方法实例,有需要的朋友可以参考一下  代码如下:         /// <summary>         /// 导出Excel         /// </summary>         /// <param name="page"></param>         /// <param name="dt"></param>  

Asp.net配合easyui实现返回json数据实例_实用技巧

本文实例讲述了Asp.net配合easyui实现返回json数据的实现方法.分享给大家供大家参考.具体如下: 最近想用asp.net配合easyui开发一个小框架,然后再用到easyui的combobox的时候出现了一个问题,总所周知,easyui的文档上给出的combobox的格式是 复制代码 代码如下: <input class="easyui-combobox"               name="language"              dat

ASP.NET中XML转JSON的方法实例_实用技巧

本文实例讲述了ASP.NET中XML转JSON的方法,分享给大家供大家参考.具体如下: 一般在许多应用程序中都将数据存储为XML的格式,而且会将数据以JSON的格式发送到客户端以做进一步处理.要实现这一点,它们必须将XML格式转换为JSON格式. XML转JSON代码如下: 复制代码 代码如下: private static string XmlToJSON(XmlDocument xmlDoc)  {      StringBuilder sbJSON = new StringBuilder(

asp.net中生成饼状与柱状图实例_实用技巧

本文实例讲述了asp.net中生成饼状与柱状图的实现方法.分享给大家供大家参考.具体方法如下: 一.生成图形的公共方法: 复制代码 代码如下: using System; using System.Collections.Generic; using System.Text; // //using System.Data; //using System.Web.UI.WebControls; // using System.Drawing; using System.Drawing.Imagin

ASP.NET中GridView和Repeater重复数据如何合并_实用技巧

这几天做一个项目有用到表格显示数据的地方,客户要求重复的数据列需要合并,就总结了一下GridView 和 Repeater 关于重复数据合并的方法. 效果图如下 : GridView : 前台代码 : <div> <asp:GridView ID="gvIncome" runat="server" AutoGenerateColumns="False"> <Columns> <asp:TemplateFi

ASP.NET中实现导出ppt文件数据的实例分享_实用技巧

前一段时间因工作需要,需增加ppt数据的导出下载.发现网络上这方面资料并不是很多,零零散散地找到一些相关的资料,经过自己的试验,终于完成相关功能.应博友要求,在此分享下我的经验,不好之处还望大家多多指出. 在做之前,首先需要添加相关引用Microsoft.Office.Interop.PowerPoint.dll. using PowerPoint = Microsoft.Office.Interop.PowerPoint; 操作PPT代码如下: 复制代码   public void creat

asp.net中datalist删除了一行数据如何自动刷新

问题描述 就是我删除花旗参那一行,之后自动显示除了花旗山的其他行 解决方案 解决方案二:不是在删除操作以后加一句bind()的函数就是刷新了么解决方案三:把绑定是方法再执行一遍解决方案四:DataBind();解决方案五:第一在才次调用绑定方法.第二,直接刷新整个页面.解决方案六:重新綁定一下數據便好解决方案七:表格和数据源绑定.从数据源中删除选中的行解决方案八:DataBind():解决方案九:DataList删除数据之后重新绑定数据源

asp.net中,update更新数据库数据出错,求指教啊!

问题描述 protectedvoidupload_Click(objectsender,EventArgse){try{if(fp_upload.PostedFile.FileName!="")//选中图片{stringfilePath=fp_upload.PostedFile.FileName;//完整路径名stringfilename=filePath.Substring(filePath.LastIndexOf("//")+1);//获取图片名称strings