Tag: webrequest

ASP.NETmultithreadingWeb请求

我正在我的ASP.NET解决方案中构建一个页面,该页面从第三方API服务中检索大部分数据。 页面本身需要对API进行大约5次单独的不同调用以填充其所有控件,因为每个API请求都会返回不同的数据集。 我想同时处理我在新线程上发出的每个单独的Web请求,以便减少加载时间。 我正在制作的每个请求都是这样的: – WebRequest request = WebRequest.Create(requestUrl); string response = new StreamReader(request.GetResponse().GetResponseStream()).ReadToEnd(); return new JsonSerializer().Deserialize(new JsonTextReader(new StringReader(response))); 首先,我应该尝试这个吗? 即,它安全吗? 并且它会通过multithreading来提高性能。 其次,这样做的最佳方法是什么? multithreading系统有许多不同的方法,但最适合这项任务的是什么?

从字符串或流上传文件到FTP服务器

我正在尝试在FTP服务器上创建一个文件,但我所拥有的只是一个字符串或数据流以及应该用它创建的文件名。 有没有办法在流或字符串上创建服务器上的文件(我没有创建本地文件的权限)? string location = “ftp://xxx.xxx.xxx.xxx:21/TestLocation/Test.csv”; WebRequest ftpRequest = WebRequest.Create(location); ftpRequest.Method = WebRequestMethods.Ftp.UploadFile; ftpRequest.Credentials = new NetworkCredential(userName, password); string data = csv.getData(); MemoryStream stream = csv.getStream(); //Magic using (var response = (FtpWebResponse)ftpRequest.GetResponse()) { }

如何在c#中使用System.Net.WebRequest设置用户代理

嗨,我正在尝试使用WebRequest设置用户代理,但不幸的是我只发现了如何使用HttpWebRequest,所以这是我的代码,我希望你可以帮助我使用WebRequest设置用户代理。 这是我的代码 public string Post(string url, string Post, string Header, string Value) { string str_ReturnValue = “”; WebRequest request = WebRequest.Create(url); request.Method = “POST”; request.ContentType = “application/json;charset=UTF-8”; request.Timeout = 1000000; if (Header != null & Value != null) { request.Headers.Add(Header, Value); } using (Stream s = request.GetRequestStream()) { using (StreamWriter sw = new StreamWriter(s)) sw.Write(Post); […]

使用C#远程HTTP发布

你如何在C#中进行远程HTTP发布(请求)?

如何以编程方式将信息发送到C#with .NET中的Web服务?

我知道这有点像重新发明轮子,但我需要知道通过http / soap / xml和Web消息与Web服务进行通信。 原因是我需要与第三方Web服务进行通信才能工作,但是WSDL或其他东西有问题,并且在使用.NET向导连接它时它不起作用。 所以,任何人都可以给我一个过程/简单的例子/等。 怎么做或任何人都可以给我一个解释它的地方的链接? 我对网络请求和响应并不十分了解。 如何构建和发送请求? 我该如何解析回复? 这是一个简单的Web服务的代码。 假装.asmx的地址是“http://www.mwebb.com/TestSimpleService.asmx”: using System; using System.Data; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; namespace TestSimpleService { [WebService] public class Soap : System.Web.Services.WebService { [WebMethod] public string SayHello(string name) { return “Hello ” + name + “!”; } } } 我怎么称呼这种方法? 任何帮助表示赞赏。 编辑 我真的只是想知道如何将数据发送到Web服务。 我可以获取所有方法/ SOAP操作/ […]

如何让普通的WebRequest异步和等待?

我需要使以下代码异步和等待。 我需要从Web服务器获取大量数据,然后这些数据将用于填充我的应用程序中的xaml页面。 所以,我需要DefLogin()方法是可以接受的。 可能吗? public void DefLogin() { postData = “My Data To Post”; var url = new Uri(“Url To Post to”, UriKind.Absolute); webRequest = WebRequest.Create(url); webRequest.Method = “POST”; webRequest.ContentType = “text/xml”; webRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), webRequest); } public void GetRequestStreamCallback(IAsyncResult asynchronousResult) { webRequest = (HttpWebRequest)asynchronousResult.AsyncState; Stream postStream = webRequest.EndGetRequestStream(asynchronousResult); byte[] byteArray = Encoding.UTF8.GetBytes(postData); postStream.Write(byteArray, 0, byteArray.Length); […]

WebRequest没有GetResponse方法 – Windows Phone 8

我想向API发送一个post请求,其中包含一些他们要求的参数…我最后只是创建了一个字符串,它很难看,但我不知道让它以不同方式工作的方法。 然后我在这个WebRequest类上找到了很多变种,但不幸的是我无法让它工作。 主要问题可能是因为我并不是真的理解这一切是如何拟合的,但基本上,我一直在使用WebRequest方法GetResponse的例子……即使在MSDN上也有这个,所以我想知道为什么当我试着调用它时在我的代码中,我没有得到那个选择? GetRequestStream 。 如何将参数添加到WebRequest中? *****DBContext() { data = “grant_type=” + GRANTTYPE + “&username=” + username + “&password=” + password + “&client_id=” + CLIENTID + “&redirect_uri=” + REDIRECTURI + “&client_secret=” + CLIENTSECRET; } public bool Authenticate() { byte[] dataStream = Encoding.UTF8.GetBytes(data); WebRequest webRequest = WebRequest.Create(urlPath); webRequest.Method = “POST”; webRequest.ContentType = “application/json”; webRequest.ContentLength = dataStream.Length; […]

c#Webrequest Post和GetResponse

我正在编写一个程序,将XML提交到网站。 编写的代码工作正常, 但有时它因某些原因停止工作,抛出System.Net.ProtocolViolationException。 我可以关闭程序并重新运行 – 它再次开始工作就好了。 这是我正在使用的代码: private string Summit(string xml) { string result = string.Empty; StringBuilder sb = new StringBuilder(); try { WebRequest request = WebRequest.Create(this.targetUrl); request.Timeout = 800 * 1000; RequestState requestState = new RequestState(xml); requestState.Request = request; request.ContentType = “text/xml”; // Set the ‘Method’ property to ‘POST’ to post data to a […]

使用C#发送POST数据

这是我尝试用来将POST数据发送到URL并恢复其响应的方法: public string sendPOST(string URL, string postData) { byte[] byteArray; Stream webpageStream; StreamReader webpageReader; String webpageContent; byteArray = Encoding.UTF8.GetBytes(postData); _webRequest = WebRequest.Create(URL); _webRequest.Method = “POST”; _webRequest.ContentType = “application/x-www-form-urlencoded”; _webRequest.ContentLength = byteArray.Length; webpageStream = _webRequest.GetResponse().GetResponseStream(); webpageStream.Write(byteArray, 0, byteArray.Length); webpageStream.Close(); webpageReader = new StreamReader(webpageStream); webpageContent = webpageReader.ReadToEnd(); return webpageContent; } 我从MSDN网页上获得了很多这样的代码,所以我知道我大致走在正确的轨道上……但是当我使用以下方法调用该方法时: string test = webHelper.sendPOST(“http://google.com”, “var=1”); MessageBox.Show(test); […]

C#从Internet Explorer获取代理设置

我在德国的某家公司遇到了问题。 他们在他们的网络中使用代理,我的程序无法与服务器通信。 IE使用此设置: 它表示:自动检测设置 这是代码: public static bool CompleteValidation(string regKey) { string uri = “***”; int c = 1; if (Counter < 5) c = 6 – Counter; string response = ""; try { System.Net.ServicePointManager.Expect100Continue = false; HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri); request.AllowWriteStreamBuffering = true; request.Method = "POST"; request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) […]