HttpGet HttpPost
public static string HttpGet(string Url, string postDataStr)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8"; HttpWebResponse response;
try
{
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException exc)
{
response = (HttpWebResponse)exc.Response;
}
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close(); return retString;
}
/// <summary>
/// 返回JSon数据
/// </summary>
/// <param name="parameter">需要传递的数据,没有则传递null</param>
/// <param name="Url">要提交的URL</param>
/// <returns>返回的数据</returns>
public static string GetResponseData(string parameter, string Url, RequestMethod method)
{
bool isWrite=false; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); request.Method = method.ToString(); if (!String.IsNullOrEmpty(parameter))
{
byte[] bytes = Encoding.UTF8.GetBytes(parameter);
request.ContentLength = bytes.Length;
request.ContentType = "text/plain";
Stream reqstream = request.GetRequestStream();
reqstream.Write(bytes, 0, bytes.Length);
} request.ServicePoint.Expect100Continue = false;
//声明一个HttpWebRequest请求
request.Timeout = 10 * 1000;
//设置连接超时时间
request.Headers.Set("Pragma", "no-cache");
HttpWebResponse response = null;
try
{
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException exc)
{
response = (HttpWebResponse)exc.Response;
isWrite = true;
} Stream streamReceive = response.GetResponseStream();
Encoding encoding = Encoding.UTF8; StreamReader streamReader = new StreamReader(streamReceive, encoding);
string strResult = streamReader.ReadToEnd();
streamReceive.Dispose();
streamReader.Dispose(); if (isWrite)
{
WriteLog.Log("HttpWebRequest异常:" + strResult, "HttpWebRequest异常.txt");
WriteLog.Log("JSONData:" + parameter, "HttpWebRequest异常.txt");
WriteLog.Log("Url:" + Url, "HttpWebRequest异常.txt");
} return strResult;
}
HttpGet HttpPost的更多相关文章
- 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 ...
- Http 四种请求访问代码 HttpGet HttpPost HttpPut HttpDelete .
String url = "http://www.baidu.com"; //将要访问的url字符串放入HttpPost中 HttpPost httpPost= new HttpP ...
- ActionResult 之HttpGet HttpPost
GET一般用于获取和查询数据. 当浏览器发送HTTP GET 请求的时候,会找到使用HttpGet限定的对应Action. POST 一般用于更新数据. 当Action上面没有限定的时候,浏览器发送任 ...
- Web Service进阶(三)HTTP-GET, HTTP-POST and SOAP的比较
XML Web Service支持三种协议来与用户交流数据.这三种协议分别是: 1.SOAP:Simple Object Access Protocol 2.HTTP-GET 3.HTTP-POST ...
- HttpGet/HttpPost请求方法
/// <summary> /// HttpGet请求 /// </summary> /// <param name="url">HttpGet ...
- Android数据与服务器交互的GET,POST,HTTPGET,HTTPPOST的使用
Android有这几种方式,可以提交数据到服务器,他们是怎么使用的呢,这里我们来探讨一下. 这里的例子用的都是提交客户端的用户名及密码,同时本节用到的StreamTools.readInputStre ...
- android 网络连接 HttpGet HttpPost方法
1.本文主要介绍利用HttpGet和HtppPost方法来获取网络json数据. 代码如下: public HttpData(String Url,HttpGetDataListener listen ...
- 译-HTTP-GET HTTP-POST SOAP protocol for ASP.NET services的异同
参考 http://stackoverflow.com/questions/4646146/http-soap-get-post https://support.microsoft.com/en-us ...
随机推荐
- python爬取拉勾网职位数据
今天写的这篇文章是关于python爬虫简单的一个使用,选取的爬取对象是著名的招聘网站--拉钩网,由于和大家的职业息息相关,所以爬取拉钩的数据进行分析,对于职业规划和求职时的信息提供有很大的帮助. 完成 ...
- 久未更 ~ 五之 —— 引入外部CSS样式表 小节
> > > > > 久未更 系列一:在html中引入外部css样式表 //引入外部css样式表 //<lilnk>要放在<head>标签的第一行, ...
- Windows7下远程操作虚拟机
⒈分别查看两台物理机的IP地址 ⒉查看虚拟机的IP地址 ⒊两台物理机和虚拟机必须都要处于同一网段,上图中我已经做过了修改(每个节点都要修改,修改见步骤6 ) ⒋打开VMware,编辑虚拟机设置(每个节 ...
- bat复制文件夹下所有文件到另一个目录
一个需求,网上了半天都是错了,所以记一下吧,方便你我. copy是文件拷贝,文件夹拷贝需要用到xcopy @echo off::当前盘符set curPath=%cd%set digPath =&qu ...
- windows下github 出现Permission denied (publickey).解决方法
今天在学习github的时候遇到了一些问题,然后爬了一会,找到了解决方法记录下来,以防忘记,当然能帮助别人最好啦! github教科书传送门:http://www.liaoxuefeng.com/wi ...
- tp5 隐藏index.php 邓士鹏
tp5 隐藏index.php ------------------------------------------------------------------------------------ ...
- MySQL时间差返回月个数
select PERIOD_DIFF(date_format(now(),'%Y%m'),date_format('2010-11-30','%Y%m')) 1. MySQL 为日期增加一个时间间隔: ...
- eclipse怎么修改工作空间路径
1.打开eclipse,在eclipse界面中的菜单栏中点击“文件”,如果您的eclipse是英文版,那么应该是file,打开后点击“切换工作空间" 2.如果您的eclipsehi英文版,则 ...
- cpp - 编译过程
预处理 E:\CppSpace\hello>g++ -o main.i -E main.cpp E:\CppSpace\hello>dir /p 驱动器 E 中的卷是 固盘-项目 卷的序列 ...
- RSync实现文件备份同步,rsync服务器
转自:http://www.cnblogs.com/itech/archive/2009/08/10/1542945.html [rsync实现网站的备份,文件的同步,不同系统的文件的同步,如果是wi ...