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. Java基础之开关语句详解

    switch 语句是单条件多分支的开关语句,它的一般格式定义如下(其中break语句是可选的): switch(表达式) { case 常量值: 若干个语句 break; case  常量值: 若干个 ...

  2. [洛谷P3979]遥远的国度

    题目大意:有一棵$n$个点的树,每个点有一个点权,有三种操作: $1\;x:$把根变成$x$ $2\;u\;v\;x:$把路径$u->v$上的点权改为$x$ $3\;x:$询问以$x$为根的子树 ...

  3. 【以前的空间】vijos 1720 阿狸的打字机

    https://www.vijos.org/p/1720 作为一个一个蒟蒻,跪了三个星期,终于在蔡大神的帮助下a了.这题网上的题解很多,不过大都把题解写的太简单了(对因为大神的题解只有三个字:傻叉题) ...

  4. [APIO2017]商旅 0/1分数规划

    ---题面--- 题解: upd: 在洛谷上被Hack了...思路应该是对的,代码就别看了 感觉有个地方还是非常妙的,就是因为在x买东西,在y卖出,就相当于直接从x走向了y,因为经过中间的城市反正也不 ...

  5. spring任务执行器与任务调度器(TaskExecutor And TaskScheduler)

    对于多线程及周期性调度相关的操作,spring框架提供了TaskExecutor和TaskScheduler接口为异步执行和任务调度.并提供了相关实现类给开发者使用.(只记录采用注解的使用形式,对于X ...

  6. POJ.1287 Networking (Prim)

    POJ.1287 Networking (Prim) 题意分析 可能有重边,注意选择最小的边. 编号依旧从1开始. 直接跑prim即可. 代码总览 #include <cstdio> #i ...

  7. LVM分区

    使用LVM对磁盘进行初始化 pvcreate /dev/vdd 创建卷组 vgcreate vg /dev/vdd 备注:vg是卷组的名称,可改变. 查看卷组的详细信息 vgdisplay 下图是我执 ...

  8. Sqlserver中如何创建链接服务器

    链接服务器在跨数据库/跨服务器查询时非常有用(比如分布式数据库系统中),我将以图文方式详细说明如何利用SQL Server Management Studio在图形界面下创建链接服务器 方法/步骤   ...

  9. Shell脚本循环读取文件中的每一行

    1.使用for循环 for line in `cat filename` do echo $line done 2.使用for循环 for line in $(cat filename) do ech ...

  10. eclipse中编写代码时如何自动提示变量名?

    打开 Eclipse  -> Window -> Perferences -> Java -> Editor -> Content Assist,在右边最下面一栏找到 a ...