using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Diagnostics;
using System.Net;
using System.IO; namespace Sample2
{
class Program
{
static void Main(string[] args)
{
try
{
//data
string cookieStr = "51fd9f14fa7561b5";
string postData = string.Format("userid={0}&password={1}", "guest", "");
byte[] data = Encoding.UTF8.GetBytes(postData); // Prepare web request...
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.xxx.com");
request.Method = "POST";
//request.Referer = "https://www.xxx.com";
request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36";
//request.Host = "www.xxx.com";
request.Headers.Add("Cookie", cookieStr);
request.ContentLength = data.Length;
Stream newStream = request.GetRequestStream(); // Send the data.
newStream.Write(data, , data.Length);
newStream.Close(); // Get response
HttpWebResponse myResponse = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
string content = reader.ReadToEnd();
Console.WriteLine(content);
Console.ReadLine();
}
catch (Exception)
{
throw;
}
}
}
}
/// <summary>
/// 调用短信接口发送短信,需配合模板
/// </summary>
/// <param name="userName">接口用户名</param>
/// <param name="userPwd">接口密码</param>
/// <param name="mobile">发送手机号码</param>
/// <param name="content">发送内容</param>
/// <returns> 返回值大于0,发送成功,系统生成的任务id或自定义的任务id</returns>
public static string PostSms(string userName, string userPwd, string mobile, string content)
{
string srcString = "-999";
try
{
string postString = string.Format("username={0}&password={1}&mobile={2}&content={3}", userName, MD5(userName + MD5(userPwd)), mobile, content);
byte[] postData = Encoding.UTF8.GetBytes(postString);//编码,尤其是汉字,事先要看下抓取网页的编码方式
string url = "http://xxx/smsSend.do";//地址
WebClient webClient = new WebClient();
webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");//采取POST方式必须加的header,如果改为GET方式的话就去掉这句话即可
byte[] responseData = webClient.UploadData(url, "POST", postData);//得到返回字符流
srcString = Encoding.UTF8.GetString(responseData);//解码
}
catch { }
return srcString;
}
    class Program
{
static void Main(string[] args)
{
var str = DownloadStringAsync(new Uri("http://www.baidu.com"));
Console.WriteLine(str.Result);
Console.ReadLine();
} static async Task<string> DownloadStringAsync(Uri uri)
{
//WebClient 不支持超时设定
//而HttpWebRequst则允许你设置请求头或者对内容需要更多的控制
var webClient = new WebClient();
webClient.Encoding = System.Text.Encoding.UTF8;
var result = await webClient.DownloadStringTaskAsync(uri);
return result;
}
}

C# 使用HttpWebRequest Post提交数据,携带Cookie和相关参数示例的更多相关文章

  1. 向后台提交数据:cookie,secure_cookie,

    向后台提交数据除了前端url,form表单,Ajax外还可以用cookie,secure_cookie,提交更多信息可以在用cookie基础上用session, cookie,secure_cooki ...

  2. C# HttpWebRequest post提交数据,提交对象

    1.客户端方法 //属于客户端 //要向URL Post的方法 public void PostResponse() { HttpWebRequest req = (HttpWebRequest)Ht ...

  3. 小范笔记:ASP.NET Core API 基础知识与Axios前端提交数据

    跟同事合作前后端分离项目,自己对 WebApi 的很多知识不够全,虽说不必要学全栈,可是也要了解基础知识,才能合理设计接口.API,方便与前端交接. 晚上回到宿舍后,对 WebApi 的知识查漏补缺, ...

  4. 允许跨域资源共享(CORS)携带 Cookie (转载)

    如何让CORS携带Cookie CORS 是一个 W3C 标准,全称是“跨域资源共享”(Cross-origin resource sharing).默认浏览器为了安全,遵循“同源策略”,不允许 Aj ...

  5. C# HttpWebRequest提交数据方式浅析

    C# HttpWebRequest提交数据方式学习之前我们先来看看什么是HttpWebRequest,它是 .net 基类库中的一个类,在命名空间 System.Net 下面,用来使用户通过HTTP协 ...

  6. C#中使用 HttpWebRequest 向网站提交数据

    HttpWebRequest 是 .NET 基类库中的一个类,在命名空间 System.Net 里,用来使用户通过 HTTP 协议和服务器交互. HttpWebRequest 对 HTTP 协议进行了 ...

  7. 使用 HttpWebRequest 向网站提交数据

    HttpWebRequest 是 .net 基类库中的一个类,在命名空间 System.Net 下面,用来使用户通过 HTTP 协议和服务器交互. HttpWebRequest 对 HTTP 协议进行 ...

  8. 【转】C# HttpWebRequest提交数据方式

    [转]C# HttpWebRequest提交数据方式 HttpWebRequest和HttpWebResponse类是用于发送和接收HTTP数据的最好选择.它们支持一系列有用的属性.这两个类位 于Sy ...

  9. 携带cookie进行数据请求

    前端进行数据请求有:普通的ajax(json)请求,jsop跨域请求,cors跨域请求,fetch请求...PC端这些请求方式中,普通的ajax(json)请求和jsop跨域请求是默认携带cookie ...

随机推荐

  1. Python 嵌套函数和闭包

    Python 嵌套函数和闭包 1.函数嵌套 如果在一个函数内部定义了另一个函数,我们称外部的函数为外函数,内部的函数为内函数,如下代码: def out_func(): def inner_func1 ...

  2. JavaScript中Switch使用

    switch 语句用于基于不同的条件来执行不同的动作.使用 switch 语句来选择要执行的多个代码块之一. switch(n) { case 1: 执行代码块 1 break; case 2: 执行 ...

  3. 如何正确实现Page接口分页,用PageImpl 自定义分页

    /** * Constructor of {@code PageImpl}. * * @param content the content of this page, must not be {@li ...

  4. BZOJ4299 & CC FRBSUM:ForbiddenSum & BZOJ4408 & 洛谷4587 & LOJ2174:[FJOI2016]神秘数——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=4299 https://www.lydsy.com/JudgeOnline/problem.php? ...

  5. cf 460 E. Congruence Equation 数学题

    cf 460 E. Congruence Equation 数学题 题意: 给出一个x 计算<=x的满足下列的条件正整数n的个数 \(p是素数,2 ≤ p ≤ 10^{6} + 3, 1 ≤ a ...

  6. bzoj 1150&2151&2288(双向链表+堆)(贪心)

    经典模型:在n个点中选k个点,要求两两不相邻,且总权值最大/最小. 做法:用双向链表串起来,把所有点丢进堆里,选择一个点的时候把它左右两个点从双向链表和堆中去除,然后把这个点的权值加进ans,出堆后改 ...

  7. lamp 源码安装

    #!/bin/bash #description:mysql-.tar apache2.4.23 php5.6.27 function check_ok(){ ] then echo "-- ...

  8. 用live()方法给新增节点绑定事件

    jQuery 给所有匹配的元素附加一个事件处理函数,即使这个元素是以后再添加进来的也有效. 这个方法是基本是的 .bind() 方法的一个变体.使用 .bind() 时,选择器匹配的元素会附加一个事件 ...

  9. All you need to know about sorting in Postgres

    按:之前看pg的执行计划,多次看到不同的排序方式,但不知何意.偶遇此篇讲解pg执行计划三种排序方式,备忘一下. Sorting Sorting is one of the most fundament ...

  10. CCPC-Winter Camp div2 day1

    A:机器人 传送门:https://www.zhixincode.com/contest/1/problem/A 题意:地图是由A.B两根线组成的,机器人一开始是在A线上的S点,他初始时可以选择任意方 ...