|
C# cs發(fā)送http get請(qǐng)求
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的數(shù)據(jù)
//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發(fā)送請(qǐng)求
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.");
}
.net 接收post請(qǐng)求
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發(fā)送圖片附件
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, "系統(tǒng)提示", 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("上傳失??!網(wǎng)絡(luò)出現(xiàn)異?;蛘邎D片文件已經(jīng)存在!","提示");
}
}
.net 接收?qǐng)D片附件文件
|
|
|
來(lái)自: 昵稱10504424 > 《C#》