HttpGet/HttpPost请求方法
/// <summary>
/// HttpGet请求
/// </summary>
/// <param name="url">HttpGet</param>
/// <returns></returns>
static string HttpGet(string url)
{
//Request
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8"; //Response
string retString = null;
using (var response = (HttpWebResponse)request.GetResponse())
{
using (var myResponseStream = response.GetResponseStream())
using (var myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")))
{
retString = myStreamReader.ReadToEnd();
}
}
return retString;
} /// <summary>
/// HttpPost请求
/// </summary>
/// <param name="url">url</param>
/// <param name="postDataStr">请求数据</param>
/// <returns></returns>
static string HttpPost(string url, string postDataStr)
{
//Request
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr);
byte[] byteData = Encoding.UTF8.GetBytes(postDataStr);
using (var myRequestStream = request.GetRequestStream())
{
myRequestStream.Write(byteData, 0, byteData.Count());
} //Response
string retString = null;
using (var response = (HttpWebResponse)request.GetResponse())
{
using (var myResponseStream = response.GetResponseStream())
using (var myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")))
{
retString = myStreamReader.ReadToEnd();
}
}
return retString;
}
HttpGet/HttpPost请求方法的更多相关文章
- Http 四种请求访问代码 HttpGet HttpPost HttpPut HttpDelete .
String url = "http://www.baidu.com"; //将要访问的url字符串放入HttpPost中 HttpPost httpPost= new HttpP ...
- ajax参数传递之[HttpGet]/[HttpPost]/[HttpPut]/[HttpDelete]请求
$.ajax({ type: "get", url: "http://localhost:27221/api/Charging/GetByModel", con ...
- httpClient Post例子,Http 四种请求访问代码 HttpGet HttpPost HttpPut HttpDelete
httpclient post方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 //----1. HttpPost request = new HttpPost(ur ...
- java最简单的方式实现httpget和httppost请求
java实现httpget和httppost请求的方式多种多样,个人总结了一种最简单的方式,仅仅需几行代码,就能够完美的实现. 此处须要用到两个jar包,httpclient-4.3.1.jar.ht ...
- httpPost请求用java代码实现的方法
原文:https://www.cnblogs.com/johnson-yuan/p/6713384.html package com.day3.sample; //首先下面我我们需要导入的jar包和文 ...
- Java实现HttpGet和HttpPost请求
maven引入JSON处理jar <dependency> <groupId>com.alibaba</groupId> <artifactId>fas ...
- android 网络连接 HttpGet HttpPost方法
1.本文主要介绍利用HttpGet和HtppPost方法来获取网络json数据. 代码如下: public HttpData(String Url,HttpGetDataListener listen ...
- [C#] 后端post的请求方法
C# 模拟post请求方法 方法1: /// <summary> /// 模拟Post请求 /// </summary> /// <param name="ur ...
- C# 接口的Get、Post、WebService请求方法一览,值得收藏
C# 接口的Get.Post.WebService请求方法一览,值得收藏 public static class HttpHelper { const string DEFAULT_USER_AGEN ...
随机推荐
- TP自适应
最近又要求职了,梳理了下这两年折腾的东西,发现有个产品很可惜,都开发完了,但是没上市.中兴的一款手表,我很喜欢那个金属壳子,结实,拿在手里沉甸甸,可以用来砸核桃. 当时调TP的时候,换了几个厂家,程序 ...
- (转)VS中的路径宏 vc++中OutDir、ProjectDir、SolutionDir各种路径说明
$(RemoteMachine) 设置为“调试”属性页上“远程计算机”属性的值.有关更多信息,请参见更改用于 C/C++ 调试配置的项目设置. $(References) 以分号分隔的引用列表被添 ...
- 2017 Multi-University Training Contest - Team 4 hdu6071 Lazy Running
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6071 题目: Lazy Running Time Limit: 2000/1000 MS (J ...
- windows 常用cmd命令
为了减少使用鼠标的频次,熟记一些常用应用的快捷键与系统本身常用的命令是必须的,以下记录一些常用的windows系统命令. 查看网络端口占用情况 :netstat -ano | findstr 8080 ...
- (译)Windows Azure:移动后端开发的主要更新
Windows Azure:移动后端开发的主要更新 这周我们给Windows Azure释出了一些很棒的更新,让云上的移动应用开发明显的简单了.这 些新功能包括: 移动服务:定制API支持移动服务:G ...
- spark学习(基础篇)--(第三节)Spark几种运行模式
spark应用执行机制分析 前段时间一直在编写指标代码,一直采用的是--deploy-mode client方式开发测试,因此执行没遇到什么问题,但是放到生产上采用--master yarn-clus ...
- Jmeter使用流程及简单分析监控(转载)
转载自:https://www.cnblogs.com/linglingyuese/archive/2013/03/04/linglingyuese-one.html#undefined 一.安装Jm ...
- $ 一步一步学Matlab(2)——Matlab基本通用操作
在上一篇中对Matlab做了一个初步的了解,本文继续来零距离亲身体验Matlab,来感受一下Matlab的一些基本.通用的操作. 命令行窗口 一打开Matlab就能看到命令行窗口,在我所用的这个精简版 ...
- JVM 内存知识总结
本文主要参考内容: http://hllvm.group.iteye.com/group/wiki/3053-JVM http://my.oschina.net/xishuixixia/blog/13 ...
- Android Camera API ISO Setting
https://stackoverflow.com/questions/2978095/android-camera-api-iso-setting exif this.mCameraParamete ...