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的更多相关文章

  1. ajax参数传递之[HttpGet]/[HttpPost]/[HttpPut]/[HttpDelete]请求

    $.ajax({ type: "get", url: "http://localhost:27221/api/Charging/GetByModel", con ...

  2. 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 ...

  3. Http 四种请求访问代码 HttpGet HttpPost HttpPut HttpDelete .

    String url = "http://www.baidu.com"; //将要访问的url字符串放入HttpPost中 HttpPost httpPost= new HttpP ...

  4. ActionResult 之HttpGet HttpPost

    GET一般用于获取和查询数据. 当浏览器发送HTTP GET 请求的时候,会找到使用HttpGet限定的对应Action. POST 一般用于更新数据. 当Action上面没有限定的时候,浏览器发送任 ...

  5. Web Service进阶(三)HTTP-GET, HTTP-POST and SOAP的比较

    XML Web Service支持三种协议来与用户交流数据.这三种协议分别是: 1.SOAP:Simple Object Access Protocol 2.HTTP-GET 3.HTTP-POST ...

  6. HttpGet/HttpPost请求方法

    /// <summary> /// HttpGet请求 /// </summary> /// <param name="url">HttpGet ...

  7. Android数据与服务器交互的GET,POST,HTTPGET,HTTPPOST的使用

    Android有这几种方式,可以提交数据到服务器,他们是怎么使用的呢,这里我们来探讨一下. 这里的例子用的都是提交客户端的用户名及密码,同时本节用到的StreamTools.readInputStre ...

  8. android 网络连接 HttpGet HttpPost方法

    1.本文主要介绍利用HttpGet和HtppPost方法来获取网络json数据. 代码如下: public HttpData(String Url,HttpGetDataListener listen ...

  9. 译-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 ...

随机推荐

  1. Tomcat服务器的下载及安装

    Tomcat服务器的下载及安装 1)到apache官网.www.apache.org     http://jakarta.apache.org(产品的主页) 2) 安装版:window (exe.m ...

  2. CSS3 background-size图片自适应

    http://www.html5cn.com.cn/css3/2013-04-21/267.html background-size属性和background-origin属性.background- ...

  3. tree conflict svn 怎么解决

    如果自己和其他人修改了同一个文件,而他已经更新到SVN,你commit时就会出现冲突,如何解决呢? 方法/步骤 使用SVN时,更新一个自己修改的文件到服务器,出现冲突,因为其他同事也修改了这个文件并且 ...

  4. 在MAC电脑上抓取iphone数据包的方法

    一.说明: 1.整个抓包操作的过程中,手机必须一直通过USB链接MAC电脑 2.手机系统要求在IOS5以上,因为使用的RVI技术在IOS5以后的系统中才有 3.抓包过程中,手机可以使用任何网络2G.3 ...

  5. 【开发技术】java异常的捕获与抛出原则

    在可能会出现exception的地方,要使用try-catch或者throws或者两者都要.我的判断依据是:如果对可能出现的exception不想被外部(方法的调用者)知道,就在方法内部try-cat ...

  6. Oracle问题之ORA-12560TNS:协议适配器错误

    Oracle问题之ORA-12560TNS:协议适配器错误 一.造成ORA-12560: TNS: 协议适配器错误的问题的原因有三个: 1.监听服务没有起起来.windows平台个一如下操作:开始-- ...

  7. tinyproxy实现https正向代理

    安装 yum install tinyproxy -y 配置 # vim /etc/tinyproxy/tinyproxy.conf ## ## tinyproxy.conf -- tinyproxy ...

  8. Redis清空数据

    进入redis目录下 redis-cli -h IP -p 端口 -a 密码 flushall

  9. ORACLE对象大小写问题

    在数据库新建一个测试表(数据库版本为ORACLE 10.2.0.1.0),表名为小写的test. 脚本如下所示: CREATE TABLE test(     id      NUMBER(10),  ...

  10. JDBC (三)

    1 数据库连接池的原理 应用程序直接获取Connection的缺点: 缺点:用户每次请求都需要向数据库获取连接,而数据库创建连接通常需要消耗相对较大的资源,创建的时间较长.如果一个网站一天访问量是10 ...