关于HttpWebRequest

using System;
using System.Collections.Generic;

using System.Net; //使用 httpwebresponse 等类库
using System.IO; // 序列化
using System.Text; //文本

namespace MyTestHttpWebRequest 
{
    public class Test
    {
        /// <summary>
        /// 消息提交
        /// </summary>
        /// <param name="str">需要提交的内容</param>
        /// <param name="url">地址</param>
        /// <returns></returns>
        public string TestHttpWebRequest(string str, string url)
        {
           HttpWebRequest httpwebrequest = WebRequest.Create(url) as HttpWebRequest; //定义

            //--填写标头--//
            httpwebrequest.ContentType = "application/x-www-form-urlencoded";//提交格式-from格式
            httpwebrequest.Accept = "*/*";
            httpwebrequest.UserAgent = "Mozilla-Firefox-Spider(Kolee)"; //服务代理

            //--提交方式--//
            httpwebrequest.Method = " POST";

            //--获取信息--//
             Stream stream = httpwebrequest.GetResponse().GetResponseStream();//获取流
            
            //------------------写入------------------//
            StreamWriter streamwriter = new StreamWriter(stream);//放入写入流
            streamwriter.Write(str);//写入流
            streamwriter.Flush();//推入流

            //------------------读取-------------------//
            StreamReader streamreader = new StreamReader(stream);//放入读取流
            string resout = streamreader.ReadToEnd(); //读取流结果

            streamwriter.Close();
            streamreader.Close();

            return resout; //返回从网页读取的返回值
        }
    }
    //这里是 提交的页面读取部分 
    public partial class _Default : System.Web.UI.Page//继承page页面
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.HttpMethod == "POST")//选定提交方式
            {
                Stream stream = Request.InputStream;//获取流
                StreamReader str = new StreamReader(stream, System.Text.Encoding.UTF8);//放入读取流
                Response.Write(str.ReadToEnd().ToString() + "<这里是接收的部分>");//输出得到的结果
                str.Close();//关闭流
                Response.Write("接收成功");
            }
        }
    }
}

 

//需要注意的是:url如果有POST提交的话。必须精确到页面

——————————————————————————————如果无法使用,可以尝试使用字节的方法———————————————————————

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace  RestApiSdk
{
public class HttpPost
{
public string SendAll(string Url, string jsonString)
{
#region 发送HTTP POST 数据

HttpWebRequest httpWebRequest = WebRequest.Create(Url) as HttpWebRequest;

httpWebRequest.Method = "POST";
httpWebRequest.ContentType = "application/json";

byte[] data = System.Text.Encoding.UTF8.GetBytes(jsonString);

using (Stream stream = httpWebRequest.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}

Stream responseStream = httpWebRequest.GetResponse().GetResponseStream();

string result = string.Empty;
using (StreamReader responseReader = new StreamReader(responseStream, Encoding.UTF8))
{
result = responseReader.ReadToEnd();
}
return result;
#endregion
}
}
}

 

posted @ 2012-07-09 10:34  瑟秋先生  阅读(779)  评论(0编辑  收藏  举报