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 ...
随机推荐
- (一)surging 微服务框架使用系列之surging 的准备工作rabbitmq安装
(1)下载erlang: http://www.erlang.org/download/otp_win64_17.3.exe 并安装 (2)下载RabbitMQ: http://www.rabbitm ...
- 怎么看vue版本
查看vue版本号是 vue -V 而不是npm vue -v ,npm vue -v 等同于npm -v vue -V: 后面那个V是大写的.
- Lnmp修改php.ini配置
http://www.chenruixuan.com/archives/341.html A-A+ 陈瑞轩2014年5月8日07102 次浏览PHP | 工作 要在lnmp系统里面修改php.ini配 ...
- 一.初识java
1.框架结构 2.main方法 3.转义符 public class Dome01 { public static void main(String[] args) { //main方法, ...
- As a Start - 毫厘之间,宇宙之外
序 突然想聊聊多重宇宙——多重宇宙,这听上去像是一个科幻概念,但是严肃思考这一个问题时,将会带我们进入一个全新的格局和世界. 对宇宙学家而言,研究多重宇宙并不仅仅是为了猜测平行世界里某一历史事件是否有 ...
- Weblogic jsp页面编译出错,Weblogic jsp编译异常
Weblogic jsp页面编译出错,Weblogic jsp编译异常 ======================== 蕃薯耀 2018年1月29日 http://www.cnblogs.com/f ...
- 浏览器http的缓存机制
原文出处-----分享从伯乐在线看到的一篇好文章 http://web.jobbole.com/85509/ 针对浏览器的http缓存的分析也算是老生常谈了,每隔一段时间就会冒出一篇不错的文章,其原 ...
- 《UNIX实用教程》读书笔记
原著:<Just Enough UNIX> Fifth Edition [美]Paul K.Andersen 译著:<UNIX实用教程> 第5版 宋虹 曾庆冬 段桂华 杨路 ...
- Linux批量ping脚本
根据鸟哥的ping脚本,我重新修改了一下.用到的知识有:read.Linux按行读取.shell输出效果调整等 其中Linux按行读取文件比较重要,可参看文下链接 1 脚本功能: 批量ping当前路径 ...
- sed 变量替换和Linux的特殊符号大全
1 sed支持两种方式的变量替换,建议使用下面的第二种,比较简单 这样就可以给变量a赋值很多特殊字符了,比如 赋值a='!@#¥%……' sed -n 's/echo/'"$a"' ...