110,566
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static string post(string url, string data)
{
string str = string.Empty;
try
{
HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(url);
byte[] buffer = Encoding.UTF8.GetBytes(data);
webReq.Method = "POST";
webReq.ContentType = "json";
webReq.ContentLength = buffer.Length;
Stream postData = webReq.GetRequestStream();
postData.Write(buffer, 0, buffer.Length);
postData.Close();
HttpWebResponse webResp = (HttpWebResponse)webReq.GetResponse();
Stream answer = webResp.GetResponseStream();
StreamReader answerData = new StreamReader(answer);
str = answerData.ReadToEnd();
}
catch (Exception ex)
{
str = string.Empty;
}
return str;
}
static void Main(string[] args)
{
string url = "http://wktv.ycoem.com/api/set_room";
string data = "user_id=301&store_id=412&key=ADFAFSFSAFASFAFA";
Console.WriteLine(post(url,data));
Console.ReadKey();
}
}
}
static void NetHttpTest()
{
using (var client = new HttpClient())
{
using (var content = new MultipartFormDataContent())
{
client.BaseAddress = new Uri("http://localhost:16724/");
var fileContent = new ByteArrayContent(File.ReadAllBytes(@"c:\work\thumb.jpg"));
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "thumb.jpg"
};
content.Add(fileContent);
fileContent = new ByteArrayContent(File.ReadAllBytes(@"c:\work\image.jpg"));
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "image.jpg"
};
content.Add(fileContent);
var dic = new Dictionary<string, string>();
dic.Add("key", "ABCDEFGH12345678");
dic.Add("id", "1");
//…… and so on
var dataContent = new FormUrlEncodedContent(dic);
content.Add(dataContent);
var result = client.PostAsync("/api/set_room", content).Result;
Console.WriteLine(result.StatusCode);
}
}
}
你多张图这样不知道对不对,用的HttpClient,要4.0版本以上才行