C# - 如何使一个HTTP调用 [英] C# - How to make a HTTP call

查看:109
本文介绍了C# - 如何使一个HTTP调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想作一个HTTP调用到网站。我只是需要打的URL,不用想上传或下载任何数据。什么是做到这一点的最简单和最快的方式。

I wanted to make an HTTP call to a website. I just need to hit the URL and dont want to upload or download any data. What is the easiest and fastest way to do it.

我试过低于code,但其缓慢和2的重复请求之后,它只是进入超时59 secounds,比简历:

I tried below code but its slow and after 2nd repetitive request it just goes into timeout for 59 secounds and than resume:

WebRequest webRequest = WebRequest.Create("http://ussbazesspre004:9002/DREADD?" + fileName);
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.ContentLength = fileName.Length;

Stream os = webRequest.GetRequestStream();
os.Write(buffer, 0, buffer.Length);
os.Close();

在使用Web客户端更有效率?

Is using the WebClient more efficient??

WebClient web = new WebClient();
web.UploadString(address);

我使用的.NET版本3.5

I am using .NET ver 3.5

推荐答案

您已经得到了一些额外的东西在里面,如果你真的只是想叫一个网站。所有你应该需要的是:

You've got some extra stuff in there if you're really just trying to call a website. All you should need is:

WebRequest webRequest = WebRequest.Create("http://ussbazesspre004:9002/DREADD?" + fileName);
WebResponse webResp = webRequest.GetResponse();

如果你不想等待响应,你可以看<一个href=\"http://msdn.microsoft.com/en-us/library/system.net.webrequest.begingetresponse.aspx\">BeginGetResponse使它异步

If you don't want to wait for a response, you may look at BeginGetResponse to make it asynchronous .

这篇关于C# - 如何使一个HTTP调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆