WinForm POST上传与后台接受

来源:http://www.sh-fengwen.com 作者:鲜果干果 人气:65 发布时间:2019-09-20
摘要:前端 转自:http://blog.csdn.net/Q_AN1314/article/details/51735184 使用jQuery从前端向后台发送JSON数据,设置了Content-Type为application/json,同时在SpringMVC的Controller里面使用了@RequestParam注解来接收参数,

前端

转自:http://blog.csdn.net/Q_AN1314/article/details/51735184


使用jQuery从前端向后台发送JSON数据,设置了Content-Type为application/json,同时在Spring MVC的Controller里面使用了@RequestParam注解来接收参数,但是只在GET请求的时候才能正常访问,在使用POST请求的时候会产生找不到参数的异常。
原因是,jQuery发送的数据是用&符号连接起来的,像是这种:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Collections.Specialized;

图片 1

namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
WebRequest webRequest = WebRequest.Create("");
public Form1()
{
InitializeComponent();
}

这里写图片描述

private void button1_Click(object sender, EventArgs e)
{
Encoding myEncoding = Encoding.GetEncoding;
//中文参数 string address = "" + HttpUtility.UrlEncode("参数一", myEncoding) + "=" + HttpUtility.UrlEncode("值一", myEncoding);
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("");
req.Method = "GET";
using (WebResponse wr = req.GetResponse
{
//在这里对接收到的页面内容进行处理
using (StreamReader reader = new StreamReader(wr.GetResponseStream(),myEncoding))
{

对应的Content-Type是 application/x-www-form-urlencoded,但是在ajax中错误地将Content-Type设置成了application/json,所以出现了这种错误。
解决方法有两种,一种是ajax中发送数据时加上双引号,这样数据的格式就是这样的:

string body = reader.ReadToEnd();
reader.Close();
MessageBox.Show;
}
}
}

图片 2

private void button2_Click(object sender, EventArgs e)
{
//post
string parmer = "act=send&name=abcd";
byte[] bytes = Encoding.ASCII.GetBytes;
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("");
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = bytes.Length;
using (Stream reqStream = req.GetRequestStream
{
reqStream.Write(bytes, 0, bytes.Length);
}
using (WebResponse wr = req.GetResponse
{
using (StreamReader sr = new StreamReader(wr.GetResponseStream
{
MessageBox.Show(sr.ReadToEnd;
}
}
}

这里写图片描述

private void button3_Click(object sender, EventArgs e)
{
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("");
req.Method = "POST";
FileStream fs = new FileStream("D:\12.jpg", FileMode.Open, FileAccess.Read);
byte[] postdatabyte = new byte[fs.Length];
fs.Read(postdatabyte, 0, postdatabyte.Length);
req.ContentLength = postdatabyte.Length;
Stream stream = req.GetRequestStream();
stream.Write(postdatabyte, 0, postdatabyte.Length);
stream.Close();
using (WebResponse wr = req.GetResponse
{
using (StreamReader sr = new StreamReader(wr.GetResponseStream
{
MessageBox.Show(sr.ReadToEnd;
}
}
}
private void button4_Click(object sender, EventArgs e)
{
UploadImage("D:\常用软件\数据库\SQL2008FULL_CHS.iso");
}

此时把Content-Type设置成application/json就可以了。另一种就是用&连接的数据,此时把Content-Type设置成application/x-www-form-urlencoded。
总之,数据的格式一定要和Content-Type保持一致。

/// <summary>
/// 通过http上传图片及传参数
/// </summary>
/// <param name="imgPath">图片地址(绝对路径:D:demoimg123.jpg)</param>
public void UploadImage(string imgPath)
{
var uploadUrl = "";
Dictionary<string, string> dic = new Dictionary<string, string>() {
{"p1",1.ToString() },
{"p2",2.ToString() },
{"p3",3.ToString() },
};
var postData = "p1=1&fname=SQL2008FULL_CHS.iso&p3=3";// Utils.BuildQuery;//转换成:para1=1¶2=2¶3=3,后台获取参数将以QueryString方式 ,而非 Form方式
var postUrl = string.Format("{0}?{1}", uploadUrl, postData);//拼接url
HttpWebRequest request = WebRequest.Create as HttpWebRequest;
request.AllowAutoRedirect = true;
request.Method = "POST";

string boundary = DateTime.Now.Ticks.ToString; // 随机分隔线
request.ContentType = "multipart/form-data;charset=utf-8;boundary=" + boundary;
byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("rn--" + boundary + "rn");
byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("rn--" + boundary

  • "--rn");

int pos = imgPath.LastIndexOf;
string fileName = imgPath.Substring;

//请求头部信息
StringBuilder sbHeader = new StringBuilder(string.Format("Content-Disposition:form-data;name="file";filename="{0}"rnContent-Type:application/octet-streamrnrn", fileName));
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString;
Stream postStream = request.GetRequestStream();
using (FileStream fs = new FileStream(imgPath, FileMode.Open, FileAccess.Read))
{
//发送分隔符
postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
//发送头部信息
postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
//写文件
byte[] fileBytes = new byte[1024];
int fileByteRead = 0;
while ((fileByteRead =fs.Read(fileBytes,0,fileBytes.Length)) !=0 )
{
postStream.Write(fileBytes, 0, fileByteRead);
}
}
//发送分隔符
postStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
postStream.Close();

HttpWebResponse response = request.GetResponse() as HttpWebResponse;
//Stream instream = response.GetResponseStream();
//StreamReader sr = new StreamReader(instream, Encoding.UTF8);
//string content = sr.ReadToEnd();
//MessageBox.Show;
using (Stream responseStream = response.GetResponseStream
{
using (StreamReader myStreamReader = new StreamReader(responseStream, Encoding.GetEncoding)
{
string retString = myStreamReader.ReadToEnd();
MessageBox.Show( retString);
}
}

本文由美高梅游戏平台网站发布于鲜果干果,转载请注明出处:WinForm POST上传与后台接受

关键词:

上一篇:AFNetworking下载文件和文件上传

下一篇:没有了

最火资讯