C#访问url地址并返回数据
public partial class Form1 : Form
{
static bool isSelect = false;
public Form1()
{
InitializeComponent();
textBox3.Enabled = textBox1.Enabled = false;
} private void button1_Click(object sender, EventArgs e)
{
if (isSelect == false)
{
textBox2.Text = GetResponseData(textBox1.Text.Trim().Replace("\n", "").Replace("\r", ""), textBox3.Text.Trim().Replace("\r", "").Replace("\n", ""));
}
else
{
textBox2.Text = HttpGet(textBox1.Text.Trim().Replace("\n", "").Replace("\r", ""));
} //textBox2.Text = HttpGet(textBox1.Text.Trim());
} public string HttpGet(string Url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8"; HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close(); return retString;
} public string GetResponseData(string JSONData, string Url)
{
byte[] bytes = Encoding.UTF8.GetBytes(JSONData);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "POST";
request.ContentLength = bytes.Length;
request.ContentType = "text/xml";
Stream reqstream = request.GetRequestStream();
reqstream.Write(bytes, , bytes.Length); //声明一个HttpWebRequest请求
request.Timeout = ;
//设置连接超时时间
request.Headers.Set("Pragma", "no-cache");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream streamReceive = response.GetResponseStream();
Encoding encoding = Encoding.UTF8; StreamReader streamReader = new StreamReader(streamReceive, encoding);
string strResult = streamReader.ReadToEnd();
streamReceive.Dispose();
streamReader.Dispose(); return strResult;
} private void label2_Click(object sender, EventArgs e)
{ } private void radioButton1_Click(object sender, EventArgs e)
{
isSelect = true;
label1.Text = "url";
textBox1.Enabled = true;
textBox3.Enabled = false;
} private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
isSelect = false;
label1.Text = "json";
textBox3.Enabled = textBox1.Enabled = true;
}
}
C#访问url地址并返回数据的更多相关文章
- 只要访问url地址 那么容器就会根据地址进行对象的创建
只要访问url地址 那么容器就会根据地址进行对象的创建
- 前端性能----从输入URL开始到返回数据的中间经历过程
这是一个古老的问题,即我们输入URL后按下回车到网页测呈现都发生了什么? 首先来看一张图: 表示了数据从源端到目的端的封装和拆解过程 预处理URL URL格式: 以http为例: http://ww ...
- FastDFS上传文件访问url地址直接下载
fdfs 存储节点storage安装nginx,修改nginx配置文件 location ~/group[1-9]/M00 { if ( $query_string ~* ^(.*)paramete ...
- 【转】命令行浏览器 curl 命令详解,Linux中访问url地址
CURL --- 命令行浏览器 这东西现在已经是苹果机上内置的命令行工具之一了,可见其魅力之一斑 1)二话不说,先从这里开始吧! curl http://www.yahoo.com 回车之后,www. ...
- 访问url地址 但tomcat会发两次请求??
statDate===2017-06-27================2017年7月11日 16:06:43执行成功,共删除0条.2017年7月11日 16:06:43执行成功,共插入48835条 ...
- thinkphp的url地址区分大小写?
在默认情况下: 在访问url地址的时候, 其中的 Action类名 即: 模块名称 是区分大小写的. (只有模块名, 即控制器名称) 可以根据设置 'URL_CASE_INSENSITIVE' =&g ...
- SQL Server 访问URL 调用WebServer
以下整理的SQL Server中访问URL地址的方法,并已封装成存储过程,可以实现POST/GET请求 SET QUOTED_IDENTIFIER ON SET ANSI_NULLS ON GO /* ...
- js进阶ajax读取json数据(ajax读取json和读取普通文本,和获取服务器返回数据(链接)都是一样的,在url处放上json文件的地址即可)
js进阶ajax读取json数据(ajax读取json和读取普通文本,和获取服务器返回数据(链接)都是一样的,在url处放上json文件的地址即可) 一.总结 ajax读取json和读取普通文本,和获 ...
- java URL实现调用其他系统发送报文并获取返回数据
模拟本系统通过Url方式发送报文到目标服务器,并获取返回数据:(实现类) import java.io.BufferedOutputStream; import java.io.BufferedRea ...
随机推荐
- 模拟 Coder-Strike 2014 - Round 1 A. Poster
题目地址:http://codeforces.com/problemset/problem/412/A /* 模拟:题目没看懂,但操作很简单,从最近的一头(如果不在一端要先移动到一端)往另一头移动,顺 ...
- MVC 使用HandleErrorAttribute统一处理异常
HandleErrorAttribute继承自FilterAttribute,且实现了IExceptionFilter接口. 属于AOP思想的一种实现,MVC的四大筛选器(权限,动作,结果,异常)中的 ...
- UITapGestureRecognizer 和 CCMenu点击问题
当一个scene中同时有UITapGestureRecognizer和CCMenu,点击时不会响应CCMenu,此时必须对UITapGestureRecognizer进行设置 UITapGesture ...
- COJ977 WZJ的数据结构(负二十三)
试题描述 输入一个字符串S,输出S的最长连续回文子串长度. 输入 输入一个字符串S. 输出 输出S的最长连续回文子串长度 输入示例 abacbbc 输出示例 4 其他说明 1<=|S|<= ...
- 使用 CJSON 在C语言中进行 JSON 的创建和解析的实例讲解
本文用代码简单介绍cjson的使用方法,1)创建json,从json中获取数据.2)创建json数组和解析json数组 1. 创建json,从json中获取数据 #include <stdio. ...
- storm在linux系统下安装调试
安装: 安装 zookeeper : 下载 zookeeper :http://zookeeper.apache.org/releases.html#download. 将 zookeeper-3.4 ...
- Memcached 笔记与总结(1)Linux(CentOS 6.6) 和 Windows(7)下安装与配置 Memcached (1.4.24)与 Memcached 基础命令
Memcached 官方网站:http://memcached.org/ 官网对其的描述是: What is Memcached? Free & open source, high-perfo ...
- Oracle数据库常用命令
导出表数据 exp user/pwd@dbname file=filename.dmp tables=tbl_name rows=y indexes=n triggers=n grants=n 导入表 ...
- phone number is not known @w@ have no phone, and thus no phone number
http://dev.mysql.com/doc/refman/5.7/en/problems-with-null.html B.5.4.3 Problems with NULL Values The ...
- mysql-insert-返回主键id
function gen_this_insert_id($insert) { GLOBAL $link; $insert .= ' SELECT LAST_INSERT_ID();'; if (mys ...