C# how to properly make a http web GET request

EDIT 23/11/17

Updated to throw out examples using async for both GET requests as well as POST


GET

public string Get(string uri)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; using(HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using(Stream stream = response.GetResponseStream())
using(StreamReader reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}

GET async

public async Task<string> GetAsync(string uri)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; using(HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync())
using(Stream stream = response.GetResponseStream())
using(StreamReader reader = new StreamReader(stream))
{
return await reader.ReadToEndAsync();
}
}

How to make HTTP POST web request

C# how to properly make a http web GET request的更多相关文章

  1. org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'PUT' not supported

    1:先上控制台报错信息 org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'PUT' not ...

  2. Web service request SetParameters to Report Server http://host/reportserver failed. Error: 请求因 HTTP 状态 401 失败: Unauthorized

    迁移CRM服务器完成后在访问CRM的内部报表时报错,在查看应用服务器的日志时发现报"Web service request SetParameters to Report Server ht ...

  3. org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported解决!

    org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported解决 ...

  4. 【Feign调用异常】org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported

    一.异常场景描述 明明是post请求,为啥到达服务器后就变成了get请求 2019-05-30 18:07:17.055 [http-nio-10650-exec-4] ERROR c.x.xcaut ...

  5. web与request

          request --> 封装了客户端所有的请求数据!       请求行       请求头       空行       请求体(GET没体) 回忆一下http协议!请求协议中的数 ...

  6. java web(四):request、response一些用法和文件的上传和下载

    上一篇讲了ServletContent.ServletCOnfig.HTTPSession.request.response几个对象的生命周期.作用范围和一些用法.今天通过一个小项目运用这些知识.简单 ...

  7. Struts2中获取Web元素request、session、application对象的四种方式

    我们在学习web编程的时候,一般都是通过requet.session.application(servletcontext)进行一系列相关的操作,request.session.和applicatio ...

  8. SoapException: Timed out while processing web services request

    情形:动态调用WebService时,语句method.Invoke异常. 异常信息为调用目标发生异常,从异常信息并不能看出问题所在,需要查看InnerException,如标题所述:处理web请求超 ...

  9. struts2获取web元素(request、session、application)

    一.Action中获取 第一种方式: 通过ActionContext,这种方式取到的对象是Map类型 import java.util.Map; import com.opensymphony.xwo ...

随机推荐

  1. 获取指定开始行数$start,跨度$limit的文件内容

    // 获取指定开始行数$page,跨度$step的文件内容 function getLine($file_name, $start, $limit) { $f = new SplFileObject( ...

  2. 7.Struts2拦截器及源码分析

    1.Struts2架构图 2.Struts2 执行过程分析 1.首先,因为使用 struts2 框架,请求被Struts2Filter 拦截 2.Struts2Filter  调用 DisPatche ...

  3. 上传图片,语音,和富文本(webuploader,dropzone, froala)

    首先是上传图片,使用的百度webuploader 自己修改后可以实例化多个uploader对象: HTML: <!DOCTYPE html> <html xmlns="ht ...

  4. VS2012隐藏输出窗口的快捷键是什么。

    纯属用键盘无法直接关闭这个窗口.有一个变通的方法是,先切换到这个输出窗口(标题呈现高亮的蓝色),使用Alt+W打开窗口菜单,选H隐藏就可以关闭.使用Ctrl+Alt+o可再次打开.按ESC就可以了.我 ...

  5. JAVA语言程序设计课后习题----第五单元解析(仅供参考)

    1 本题是水题,题目要求你求最大值.最小值,建议你用Arrays.sort函数进行排序,最大值.最小值就可以确定了 import java.util.Arrays; import java.util. ...

  6. remote mounting from windows to linux

    8 Ways To Mount SMBfs (SAMBA FILE SYSTEM) In Linux. Sep 8, 2009 How to Mount smbfs (SAMBA file syste ...

  7. Delphi MSComm控件事件的介绍

  8. centos6.4升级openssh7.4p1

    Centos6.4版本yum升级openssh版本最高到5.3,想要升级到更高的版本需要重新编译 一.查看当前openssh版本: [root@localhost ~]# ssh -VOpenSSH_ ...

  9. deep_learning_Function_list变量前面加星号,字典变量前面加两个星号

    列表前面加星号作用是将列表解开成两个独立的参数,传入函数, 字典前面加两个星号,是将字典解开成独立的元素作为形参. def add(a, b): return a+b data = [4,3] pri ...

  10. Html中使用Cookie取值赋值

    //设置Cookie function setCookie(name, value) { var Days = 1; var exp = new Date(); exp.setTime(exp.get ...