zoukankan
html css js c++ java
使用 HttpRequest Get和Post调用其他页面
昨天选课,虽然有一个排队系统。但是排队系统实在不怎么的啊。插队的有那么的多。有人做了插队工具下了一个看看,
那是一个使用javascript实现的系统,整个只有一个页面。你可以在这里下载:
/Files/lulu/xk.rar
。它使用xmlHttpRequest向服务器发送请求。
js不太熟悉,发现HttpRequest功能差不多,用它试试啦。
但是不知道提交数据怎么组织,革命尚未成功但是还是学到了点东西,就在这了写下来了:
//
Get请求方式
private
string
RequestGet(
string
Url)
{
string
PageStr
=
string
.Empty;
//
用于存放还回的html
Uri url
=
new
Uri(Url);
//
Uri类 提供统一资源标识符 (URI) 的对象表示形式和对 URI 各部分的轻松访问。就是处理url地址
try
{
HttpWebRequest httprequest
=
(HttpWebRequest)WebRequest.Create(url);
//
根据url地址创建HTTpWebRequest对象
参数设置
#region
参数设置
httprequest.Method
=
"
get
"
;
//
---------------------------------------------设定一些参数(不必要可以)
//
httprequest.KeepAlive = false;
//
持久连接设置为false
//
httprequest.ProtocolVersion = HttpVersion.Version11;
//
网络协议的版本
//
httprequest.Proxy = WebProxy.GetDefaultProxy();
//
服务器代理
//
httprequest.ContentType = "application/x-www-form-urlencoded";
//
http 头
//
httprequest.AllowAutoRedirect = true;
//
httprequest.MaximumAutomaticRedirections = 10;
//
httprequest.Timeout = 30000;
//
设定超时十秒(毫秒)
//
httprequest.UserAgent = "mozilla/4.0 (compatible; msie 6.0; windows nt 5.1)";
//
浏览器
//
=================================================
#endregion
HttpWebResponse response
=
(HttpWebResponse)httprequest.GetResponse();
//
使用HttpWebResponse获取请求的还回值
Stream steam
=
response.GetResponseStream();
//
从还回对象中获取数据流
StreamReader reader
=
new
StreamReader(steam, Encoding.GetEncoding(
"
gb2312
"
));
//
读取数据Encoding.GetEncoding("gb2312")指编码是gb2312,不让中文会乱码的
PageStr
=
reader.ReadToEnd();
reader.Close();
}
catch
(Exception e)
{
PageStr
+=
e.Message;
}
return
PageStr;
}
//Post请求方式,于Get的方式写法相似,所以解释就些少一点了
private
string
RequestPost(
string
Url,
string
Context)
//
两个参数分别是Url地址和Post过去的数据
{
string
PageStr
=
string
.Empty;
Uri url
=
new
Uri(Url);
byte
[] reqbytes
=
Encoding.ASCII.GetBytes(Context);
try
{
HttpWebRequest req
=
(HttpWebRequest)WebRequest.Create(url);
req.Method
=
"
post
"
;
req.ContentType
=
"
application/x-www-form-urlencoded
"
;
req.ContentLength
=
reqbytes.Length;
Stream stm
=
req.GetRequestStream();
stm.Write(reqbytes,
0
, reqbytes.Length);
stm.Close();
HttpWebResponse wr
=
(HttpWebResponse)req.GetResponse();
Stream stream
=
wr.GetResponseStream();
StreamReader srd
=
new
StreamReader(stream,Encoding.GetEncoding(
"
gb2312
"
));
PageStr
+=
srd.ReadToEnd();
stream.Close();
srd.Close();
}
catch
(Exception e)
{
PageStr
+=
e.Message;
}
return
PageStr;
}
查看全文
相关阅读:
满20年程序员生涯-与大家分享最近7年的快速成长经历(上海市青浦区快递行业战斗7年奋斗史)
格局 逐阶而上
基础才是重中之重~BouncyCastle实现的DES3加密~java通用
jenkins~Publish Over SSH实现分布式部署
maven~为MANIFEST.MF文件添加内容
maven~多个plugin相同phase的执行顺序
java~jar防止反编译
个人博客的简单通告
SQL Server中datetimeset转换datetime类型问题浅析
MySQL如何计算统计redo log大小
原文地址:https://www.cnblogs.com/xxaxx/p/2785547.html
最新文章
[LeetCode] 973. K Closest Points to Origin 最接近原点的K个点
[LeetCode] 972. Equal Rational Numbers 相等的有理数
[LeetCode] 971. Flip Binary Tree To Match Preorder Traversal 翻转二叉树以匹配先序遍历
[LeetCode] 970. Powerful Integers 强力数字
[LeetCode] 969. Pancake Sorting 煎饼排序
[LeetCode] 968. Binary Tree Cameras 二叉树相机
[LeetCode] 967. Numbers With Same Consecutive Differences 连续差相同的数字
[LeetCode] 966. Vowel Spellchecker 元音拼写检查器
[LeetCode] 987. Vertical Order Traversal of a Binary Tree 竖直遍历二叉树
[LeetCode] 965. Univalued Binary Tree 单值二叉树
热门文章
[LeetCode] 964. Least Operators to Express Number 表示数字的最少运算符
博客园x丝芙兰-圣诞元旦特别活动:双旦选礼,美力送递团队
上周热点回顾(12.21-12.27)团队
【故障公告】redis内存耗尽造成博客后台无法保存团队
上周热点回顾(12.14-12.20)团队
园子的进化:博客园x丝芙兰,build更美的你团队
上周热点回顾(12.7-12.13)团队
集思广益:博客园卫衣设计初稿出炉团队
故障公告:黑色星期四(暂时恢复正常)团队
博客园用户注册Amazon Web Service (AWS) 12月特别活动团队
Copyright © 2011-2022 走看看