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");
}