我正在尝试开发一个微软excel插件发送excel表格数据到web应用程序。它将要求插件提示用户名和密码,然后向web应用程序发送登录http请求以获得会话。然后它会将数据上传到web应用程序。我应该使用什么.net的东西?
发布于 2011-07-21 02:38:11
发送用户名和密码的Post方法示例。要上传文件,只需在google.com或bing.com中搜索"File Upload C#“或尝试C#'s WebClient.UploadFile, Code Project
// 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(); 发布于 2011-07-21 02:22:35
对于.NET中的HTTP通信,可以尝试System.Net.WebClient或System.Net.HttpWebRequest。
https://stackoverflow.com/questions/6766536
复制相似问题