zoukankan      html  css  js  c++  java
  • 关于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
    }
    }
    }
  • 相关阅读:
    java final修饰符
    java getClass()
    java color类简介
    JAVA本地方法详解,什么是JAVA本地方法?
    this 关键字
    main 静态方法 非静态方法
    Integer与int
    java创建一个对象时,内存中发生了什么
    java中静态变量在内存中的位置
    java使用new Date()和System.currentTimeMillis()获取当前时间戳
  • 原文地址:https://www.cnblogs.com/QQ544952425/p/2582309.html
Copyright © 2011-2022 走看看