post方式模拟表单向服务器提交数据

<pre name="code" class="java"><span style="font-size:18px;">import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpConnection;
//模拟用户在网页提交表单
public class HttpUtils
{
	//向服务器发送表单数据,接受返回的数据
	public static String sendFormData(String path , HashMap<String,String> params,String code) throws IOException
	{
		
		URL url = new URL(path);
		HttpURLConnection con = (HttpURLConnection) url.openConnection();
		
		//得到要提交的数据
		byte[] data = getFormData(params, code);
		con.setRequestMethod("POST");
		con.setConnectTimeout(3000);
		con.setDoInput(true);
		con.setDoOutput(true);
		con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
		con.setRequestProperty("Content-Length", data.length+"");
		
		OutputStream out = con.getOutputStream();
		out.write(data);
		out.close();
		InputStream in=null;
		if(con.getResponseCode()==200)
		{
			in = con.getInputStream();
			return getReturnData(in, code);
		}
		return null;
	}
		//把表单数据转换为用来传递的数据格式
		public static byte[] getFormData(HashMap<String,String> params,String code) throws UnsupportedEncodingException
		{
			StringBuilder sb = new StringBuilder();
			for(Map.Entry<String,String> en:params.entrySet())
			{
				sb.append(en.getKey())
				.append("=")
				.append(URLEncoder.encode(en.getValue(),code))
				.append("&");
			}
			sb.deleteCharAt(sb.length()-1);
			return sb.toString().getBytes(code);
		}
		//得到 从服务器返回的数据按指定编码解码成的字符串
		public static String getReturnData(InputStream in,String code) throws IOException
		{
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			byte[] b=new byte[1024];
			int len=0;
			while((len = in.read(b))!=-1)
			{
				baos.write(b,0,len);
			}
			return new String(baos.toByteArray(),code);
		}
}










</span>


 
 
 

                
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 在原生ajax中,可以通过以下步骤来提交form表单数据进行post请求。首先,获取form表单元素并将其转换为formData表单对象。然后,创建一个XMLHttpRequest对象,并配置它的请求方式和URL。如果需要发送跨域请求并携带cookie信息,可以设置xhr的withCredentials属性为true。接下来,发送请求并传递formData作为请求参数。最后,监听服务器端给予的响应内容,可以通过xhr的onload事件来获取响应数据。\[1\] 在jQuery中,可以通过以下步骤来提交form表单数据进行post请求。首先,获取form表单元素并将其赋值给一个变量。然后,使用$.ajax方法发送post请求,配置请求的类型、URL和数据。如果需要处理formData类型的数据,可以设置processData和contentType属性为false。最后,可以在success回调函数中处理服务器端返回的响应数据,或在error回调函数中处理请求失败的情况。\[2\] 需要注意的是,无论是原生ajax还是jQuery,如果要模拟form表单提交数据,需要在发送请求前设置请求头的Content-Type为"application/x-www-form-urlencoded",并将表单中的数据以键值对的形式拼接成字符串,并作为send函数的参数发送。\[3\] #### 引用[.reference_title] - *1* *2* [通过ajax提交form表单数据的几种方式](https://blog.csdn.net/chenshanqiang/article/details/103934308)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [AJAX学习笔记——发送AJAX的POST请求,模拟from表单提交](https://blog.csdn.net/weixin_62117675/article/details/127816648)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值