C# cs发送http get请求
try
{
WebRequest req = WebRequest.Create("http://127.0.0.1/test/loginsso.aspx?username=admin&password=admin");
req.Method = "POST"; //指定提交的Method,可以为POST和GET,一定要大写
//byte[] postData = System.Text.Encoding.GetEncoding("gbk").GetBytes("?username=admin&password=admin");//Post的数据
//req.ContentLength = postData.Length;
Stream postStream = req.GetRequestStream();
//postStream.Write(postData, 0, postData.Length);
postStream.Close();
WebResponse res = req.GetResponse();
System.Text.Encoding resEncoding = System.Text.Encoding.GetEncoding("utf-8");//接收的编码
StreamReader reader = new StreamReader(res.GetResponseStream(), resEncoding);
string html = reader.ReadToEnd(); //接收的Html
MessageBox.Show("=========" + html);
reader.Close();
res.Close();
}
catch (Exception ex)
{
MessageBox.Show("error");
}
.net 接收get发送请求
Response.ContentEncoding = Encoding.GetEncoding("UTF-8");
string username = Request["username"];
string password = Request["password"];
if (username != "" && username == "admin" && password != "" && password == "admin")
{
Response.Write("success");
}
else
{
Response.Write("error" + Request.Url.Host);
// Response.Redirect("http://www.g.cn");
}
.net 接收post请求
System.Text.Encoding resEncoding = System.Text.Encoding.GetEncoding("utf-8");//接收的编码
StreamReader reader = new StreamReader(Request.InputStream, resEncoding);
string msg = reader.ReadToEnd();
reader.Close();
c# cs发送图片附件
if (!textBox_fileName.Text.Trim().Equals(""))
{
string loadFile = textBox_fileName.Text.Trim();
string fileName = loadFile.Substring(loadFile.LastIndexOf("\\") + 1, loadFile.Length - 1 - loadFile.LastIndexOf("\\"));
string urlStr = @"http://127.0.0.1/test/UploadFile.aspx?name=" + fileName;
UploadFileBinary(loadFile, urlStr);
}
else
{
string alStr = "您还没有选择文件";
MessageBox.Show(alStr, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
}
public void UploadFileBinary(string localFile, string uploadUrl)
{
try
{
FileStream rdr = new FileStream(localFile, FileMode.Open);
byte[] inData = new byte[4096];
int totbytes = 0;
MemoryStream postData = new MemoryStream();
int bytesRead = rdr.Read(inData, 0, inData.Length);
while (bytesRead > 0)
{
postData.Write(inData, 0, bytesRead);
bytesRead = rdr.Read(inData, 0, inData.Length);
totbytes += bytesRead;
}
rdr.Close();
postData.Position = 0;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uploadUrl);
req.Method = "POST";
req.ContentLength = (long)postData.Length;
using (Stream s = req.GetRequestStream())
{
s.Write(postData.ToArray(), 0, (int)postData.Length);
postData.Close();
}
WebResponse resp = req.GetResponse();
System.Text.Encoding resEncoding = System.Text.Encoding.GetEncoding("utf-8");//接收的编码
StreamReader reader = new StreamReader(resp.GetResponseStream(), resEncoding);
string msg = reader.ReadToEnd();
reader.Close();
resp.Close();
if (msg != null && msg.Equals("success"))
{
MessageBox.Show("图片上传成功","提示");
}
}
catch (Exception ex)
{
//string exContent;
// exContent = ex.ToString();
MessageBox.Show("上传失败!网络出现异常或者图片文件已经存在!","提示");
}
}
.net 接收图片附件文件
Response.ContentEncoding = Encoding.GetEncoding("UTF-8");
// 在此处放置用户代码以初始化页面
byte[] theData = null;
String ls_name;
if (Request.ServerVariables["REQUEST_METHOD"].ToString().ToUpper() == "POST")
{
theData = Request.BinaryRead(Request.ContentLength);
//获取文件名称
ls_name = Request.QueryString["name"];
//string picName = DateTime.Now.Ticks.ToString() + ".gif";
//string picName = DateTime.Now.Ticks.ToString() + ".jpg";
FileStream stm = new FileStream(Server.MapPath("uploadfile/"+ls_name), System.IO.FileMode.CreateNew);
stm.Write(theData, 0, (int)theData.Length);
stm.Close();
Response.Write("success");
}
else
{
Response.Write("error");
}
分享到:
相关推荐
该压缩包内含有 BS版本和CS 版本的 多文本网页编辑控件。 含详细介绍,未加密。可反编译修改源码。
C# 使用Get或Post请求获取数据 并解析获取到的Json数据
C#WinForm基于BS构架实现CS实例及源代码.zipC#WinForm基于BS构架实现CS实例及源代码.zipC#WinForm基于BS构架实现CS实例及源代码.zipC#WinForm基于BS构架实现CS实例及源代码.zip 本人QQ:280067279,加时注明:CSDN,我...
CS、BS 获取文件路径及读取文件内容的示例代码
BS与CS的websocket连接
C#-HTTP请求接收(Post数据流,GET,表单)
C#WinForm基于BS构架实现CS实例及源代码 这个都是本人收藏了3年的资源 现放出 都是总结了很多系统 软件项目实施过程中的经验的 慢慢积累的
本人花了三天,看了很多网络资料,一直没有找到有关c#与组态王DDE的代码,也看见很多人在问,我自已做了一个dll,用c#调用之,可以实现,现将c#调用示例及dll奉上,但愿能帮有需要的朋友一些忙,有什么问题,可以qq我...
此软件可以把cs程序转换成bs的,在压缩包里有使用说明,按照说明操作。本程序转换的bs是asp.net的,需要运行于iis和.net框架。
C# http Get/POST请求封装类
C#邮件功能,发和接,cs和bs的 有什么疑问 可以 桌子我的空间里留言
C#BS模式导出GridView导出和获取数据信息
c# rest http请求类库c# rest http请求类库c# rest http请求类库
基于.net下C#开发的BS模式下网络考试平台(源码)含系统设计解决方案文档.zip
c#CS登录注册实现ersion2.0 c#CS登录注册实现ersion2.0
C#抓包,SnifferHttp使用网卡获取Http请求数据,可以用于监控网卡数据包。
一个不错的BS框架源码(c#) 一个不错的BS框架源码(c#) 框架程序
C#语言,基于CS架构,三层架构(界面、业务、数据) 设计实例描述
Net的BS与CS通信源码,为采用cs和bs之间通信,亲测可用