对WEB url 发送POST请求
package com.excellence.spark; import java.util.List;
import com.excellence.spark.test.test;
import com.sun.xml.internal.bind.v2.schemagen.xmlschema.Import;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.AllowAllHostnameVerifier;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import net.sf.json.JSONObject; import java.io.IOException;
import java.net.URLDecoder; /** 对指定url发送post的指定内容
* @author 947
*
*/
public class HttpRequest {
/**
*
* @param url url
* @param content 发送post的内容
* @return
*/
public static String httpPost(String url,String content){
DefaultHttpClient httpClient = new DefaultHttpClient();
JSONObject jsonResult = new JSONObject(); HttpPost method = new HttpPost(url);
try {
if (null != content) {
StringEntity entity = new StringEntity(content, "utf-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
method.setEntity(entity);
}
HttpResponse result = httpClient.execute(method); url = URLDecoder.decode(url, "UTF-8"); String entity = EntityUtils.toString(result.getEntity()); // 拿到响应的实体的字符串 System.out.println(entity);
return entity; } catch (IOException e) {
e.printStackTrace();
return null;
}
} public static void main(String[] args) {
String url = "http://127.0.0.1:8000/index/";
JSONObject jsonObject = new JSONObject();
jsonObject.put("word", "111111");
String result = HttpRequest.httpPost(url, jsonObject.toString()); // 返回响应结果
System.out.println(result);
}
}
如何在python中拿到这个request然后并响应返回response呢???
#-*-coding:utf-8-*- from importlib import reload
import json
from django.shortcuts import render
from django.shortcuts import HttpResponse
import requests
from sendPostText.predict import CnnModel def index(request):
cnn_model = CnnModel() received_json_data = json.loads(request.body)
word = received_json_data['word'] result = cnn_model.predict(word) data = {'word':result} data = json.dumps(data,ensure_ascii=False) # 这里加入ensure_ascil=False保证中文不乱码
response = HttpResponse(data,content_type="application/json,charset=UTF-8")
return response
对WEB url 发送POST请求的更多相关文章
- 向指定URL 发送POST请求的方法
java发送psot请求: package com.tea.web.admin; import java.io.BufferedReader; import java.io.IOException; ...
- python爬微信公众号前10篇历史文章(2)-拼接URL&发送http请求
如何拼接想要的url http://weixin.sogou.com/weixin?type=1&page=1&ie=utf8&query=%E5%A4%A7%E7%BA%BD ...
- java通过java.net.URL发送http请求调用接口
一般在*.html,*.jsp页面中我们通过使用ajax调用接口,这个是我们通常用的.对于这些接口,大都是本公司写的接口供自己调用,所以直接用ajax就可以.但是,如果是多家公司共同开发一个东西,一个 ...
- Hbuilder MUI里面使用java.net.URL发送网络请求,操作cookie
1. 引入所需网络请求类: var URL = plus.android.importClass("java.net.URL"); var URLConnection = plus ...
- Ajax在静态页面中向指定url发送json请求获取返回的json数据
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- 【jQuery】JS中拼接URL发送GET请求的中文、特殊符号的问题
> 参考的优秀文章 jQuery ajax - param() 方法 经常,我们需要在JS中拼接URL然后以GET形式提交请求.如果遇到中文.特殊符号则需要作各种处理. jQuery有一个方法非 ...
- HTTP 笔记与总结(3 )socket 编程:发送 GET 请求
使用 PHP + socket 模拟发送 HTTP GET 请求,过程是: ① 打开连接 ② 构造 GET 请求的数据:写入请求行.请求头信息.请求主体信息(GET 请求没有主体信息) ③ 发送 GE ...
- JAVA使用apache http组件发送POST请求
在上一篇文章中,使用了JDK中原始的HttpURLConnection向指定URL发送POST请求 可以看到编码量有些大,并且使用了输入输出流 传递的参数还是用“name=XXX”这种硬编的字符串进行 ...
- socket编程发送GET请求
可以根据几根url地址,分析出主机,地址,协议等,然后用封装成的类拼接成GET请求信息,用fsockopen连接主机,进行读取操作,获取响应信息,打印 <?php //http连接接口 inte ...
随机推荐
- GoLand(一)安装
Infi-chu: http://www.cnblogs.com/Infi-chu/ 一.安装包下载地址https://golang.org/ 二.Windows下安装:1.下载好.msi的安装包文件 ...
- 20155238 2016-2017-2 《Java程序设计》第二周学习总结
教材学习内容总结 java基本类型:整数,字节,浮点数,字符 //"单行批注" */"单行批注" 变量 "驼峰式命命法" int age0f ...
- CodeForces 985D Sand Fortress
Description You are going to the beach with the idea to build the greatest sand castle ever in your ...
- SpringCloud-微服务的注册与发现Eureka(二)
一.SpringCloud简介 Spring Cloud是一系列框架的有序集合.它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册.配置中心.消息总线.负载均 ...
- libpcap详解
转发自http://blog.chinaunix.net/uid-21556133-id-120228.html libpcap(Packet Capture Library),即数据包捕获函数库,是 ...
- [SYZOJ279]滑♂稽♂树
主♂席♂树♂裸♂题 https://syzoj.com/problem/279 https://oj.changjun.com.cn/problem/detail/pid/2425 // It is ...
- JS如何捆绑TypeScript声明文件
前话 TypeScript是JavaScript类型的超集 这是TypeScript的文档介绍的一句话,那么他们存在联系呢? 我的理解是,TypeScript在JavaScript基础上引入强类型语言 ...
- Flutter - Stateful(有状态) 和 stateless(无状态) widgets
Stateful(有状态) 和 stateless(无状态) widgets 有些widgets是有状态的, 有些是无状态的 如果用户与widget交互,widget会发生变化,那么它就是有状态的. ...
- CSS过渡动画之transition
O(∩_∩)O~ 这两天在看看CSS的相关内容,关于transition动画感觉很有意思,分享一下. CSS负责给html加效果,自然少不了各种动画,今天介绍一下transition. 概述 看一段比 ...
- selenium+java多层级frame切换的问题
关于selenium多层iframe切换,及iframe没有id和name属性的情况下进行切换的问题.(如下图:) 问题: 1. 在切入到frame:left中后,直接切换其他同级和上级frame报错 ...