public ActionResult Index()
{
var model = base.RenderOutPutModel();
//string url = "http://localhost:46145/ServerQueue";
//SendDTO SendDTO = new SendDTO();
//SendDTO.CallbackId = "sdsfasdfsdfsadfsdfsad22222";
//SendDTO.Account = "123";
//SendDTO.Password = "123";
//SendDTO.CallbackUrl = "http://localhost:46145/DataExchange/DataExchange?id=&ss=";
//HttpHelper.PostDataHtml(url, System.Web.HttpUtility.UrlEncode(JsonConvert.SerializeObject(SendDTO)));//编码+序列化
return View(model);
}
/// <summary>
/// HttpWebRequest 通过Post
/// </summary>
/// <param name="url">URI</param>
/// <param name="postData">post数据</param>
/// <returns></returns>
public static void PostDataHtml(string url, string body)
{
UTF8Encoding encoding = new UTF8Encoding();
byte[] postData = encoding.GetBytes(body);
var uri = new Uri(url);
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(uri);
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = postData.Length;
myRequest.PreAuthenticate = true;
myRequest.AllowWriteStreamBuffering = false;
myRequest.SendChunked = false;
myRequest.KeepAlive = true;
myRequest.Timeout = int.MaxValue;
myRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
myRequest.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes("admin:admin")));
//身份凭证
CredentialCache cc = new CredentialCache();
cc.Add(uri, "Basic", new NetworkCredential("admin", "admin"));
myRequest.Credentials = cc;
//发送数据
Stream newStream = myRequest.GetRequestStream();
// Send the data.
newStream.Write(postData, 0, postData.Length);
newStream.Flush();
newStream.Close();
//返回响应
HttpWebResponse myResponse;
myResponse = (HttpWebResponse)myRequest.GetResponse();
if (myResponse != null && myResponse.StatusCode == HttpStatusCode.OK)
{
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
var str = reader.ReadToEnd();
}
}
接收方法
public JsonResult Index()
{
byte[] byts = new byte[Request.InputStream.Length];
Request.InputStream.Read(byts, 0, byts.Length);
string data = System.Text.Encoding.Default.GetString(byts);
data = Server.UrlDecode(data);
SendDTO dto = JsonConvert.DeserializeObject<SendDTO>(data);
}