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 ...
随机推荐
- Spider_Man_4 の BeautifulSoup
一 介绍 Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup会帮你 ...
- 2. Event编写
Event作用: 存储事件数据. IEventBase K:是Actor的StateId的类型,可以是long.可以是string,Ray一般使用OGuid生成的字符串作为主键. 编写Event继承I ...
- JavaScript八张思维导图—编程风格
JS基本概念 JS操作符 JS基本语句 JS数组用法 Date用法 JS字符串用法 JS编程风格 JS编程实践 不知不觉做前端已经五年多了,无论是从最初的jQuery还是现在火热的Angular,Vu ...
- iframe及与页面之间的通信
获取iframe对象 iframe元素本身是位于父级页面中的,所以你可以像一个普通元素一样的使用和操作它 代表了iframe内容window对象是作为一个页面的属性加入到iframe中的, 为了让父级 ...
- nginx https配置后无法访问,可能防火墙在捣鬼
同事发现nginx配置后https 无法访问,我帮忙解决的时候从以下出发点 1.防火墙未开放443端口 2.配置出错 1 2 3 于是就 netstat -anp 查看防火墙开的端口 发现已经在监听了 ...
- VS code注释快捷键
注释: 先CTRL+K,然后CTRL+C 取消注释: 先CTRL+K,然后CTRL+U
- Codeforces 895C - Square Subsets 状压DP
题意: 给了n个数,要求有几个子集使子集中元素的和为一个数的平方. 题解: 因为每个数都可以分解为质数的乘积,所有的数都小于70,所以在小于70的数中一共只有19个质数.可以使用状压DP,每一位上0表 ...
- LindDotNetCore~Polly组件对微服务场景的价值
回到目录 Polly是一个开源框架,在github上可以找到,被善友大哥收录,也是.App vNext的一员! App vNext:https://github.com/App-vNext GitHu ...
- PL/SQL FAQ in installation "make sure you have the 32 bits Oracle client installed" and "Database character set(AL32UTF8) and Client character set (GBK) are different"
requirement : connecting to remote oracle server . now I know the connectionURL :connectionUrl :jdb ...
- Django 中 makemigrations、migrate时 No changes detected
Django创建的项目中,需要更改.增加.删除表中的某些属性,性急直接把之前数据库表删除了,之后再执行: python manage.py makemigrations python manage.p ...