zoukankan      html  css  js  c++  java
  • httpwebRequest post 数据

    post 数据过去

    private string Post(string strURL, string paramString)
            {
                string pagehtml = string.Empty;
                try
                {
                    Encoding myEncoding = Encoding.GetEncoding("UTF-8");
                    Uri myUri = new Uri(strURL);
                    byte[] paramBytes = myEncoding.GetBytes(paramString);
                    HttpWebRequest myWebRequest = (HttpWebRequest)WebRequest.Create(myUri);
                    myWebRequest.ContentType = "application/x-www-form-urlencoded";
                    myWebRequest.AllowAutoRedirect = true;
                    myWebRequest.Method = "POST";
                    myWebRequest.Timeout = 60000;
                    myWebRequest.ContentLength = paramBytes.Length;

                    //Send request
                    Stream requestStream = myWebRequest.GetRequestStream();
                    requestStream.Write(paramBytes, 0, paramBytes.Length);
                    requestStream.Close();

                   //or we can user below

                   

    //或者
      using (Stream postStream =myWebRequest.GetRequestStream()) {
                        using (StreamWriter writer = new StreamWriter(postStream)) {
                            writer.Write(requestPayload);
                        }
                    } 

                    //Get response
                    HttpWebResponse myWebResponse = (HttpWebResponse)myWebRequest.GetResponse();
                    StreamReader myStreamReader = new StreamReader(myWebResponse.GetResponseStream(), myEncoding);
                    pagehtml = myStreamReader.ReadToEnd();
                    myStreamReader.Close();
                }
                catch (Exception ex)
                {
                    return string.Empty;
                }            
                return pagehtml;
            }      

    响应post过来的数据

    如果是XML

    XML = Request["xml"].ToString();

    为response 信息添加头

    Response.AppendHeader(
                    "X-XRDS-Location",
                    new Uri(Request.Url, Response.ApplyAppPathModifier("~/xrds3")).AbsoluteUri);
           
  • 相关阅读:
    .NET 开源GIS解决方案一 概述
    未能正确加载“Microsoft.VisualStudio.Editor.Implementation.EditorPackage”包
    C# Winform欢迎窗体实现()
    Log4Net使用指南(转)
    DotNet经典面试题(转载)
    这是一篇测试文章
    每天进步一点点
    博客园公告栏添加两个有趣的控件
    eclipse下查看maven下载的源码中文乱码问题
    WEB 基础知识(一)
  • 原文地址:https://www.cnblogs.com/huhu456/p/2280233.html
Copyright © 2011-2022 走看看