首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >查找发送http请求的c#方式

问查找发送http请求的c#方式
EN

Stack Overflow用户
提问于 2011-07-21 02:20:00
回答 2查看 9.9K关注 0票数 2

我正在尝试开发一个微软excel插件发送excel表格数据到web应用程序。它将要求插件提示用户名和密码,然后向web应用程序发送登录http请求以获得会话。然后它会将数据上传到web应用程序。我应该使用什么.net的东西?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-07-21 02:38:11

发送用户名和密码的Post方法示例。要上传文件,只需在google.com或bing.com中搜索"File Upload C#“或尝试C#'s WebClient.UploadFile, Code Project

代码语言:javascript
复制
// Create a request using a URL that can receive a post. 
WebRequest request = WebRequest.Create("http://example.com");
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
string postData = "username=user&passsword=pass";
byte[] byteArray = Encoding.UTF8.GetBytes (postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream ();
// Write the data to the request stream.
dataStream.Write (byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close ();
// Get the response.
WebResponse response = request.GetResponse ();
// Display the status.
Console.WriteLine (((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream ();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader (dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd ();
// Display the content.
Console.WriteLine (responseFromServer);
// Clean up the streams.
reader.Close ();
dataStream.Close ();
response.Close();                                                                                         
票数 5
EN

Stack Overflow用户

发布于 2011-07-21 02:22:35

对于.NET中的HTTP通信,可以尝试System.Net.WebClient或System.Net.HttpWebRequest。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6766536

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档