对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 ...
随机推荐
- 大数据入门第二天——基础部分之zookeeper(上)
一.概述 1.是什么? 根据凡技术必登其官网的原则,我们先去官网瞅一瞅:http://zookeeper.apache.org/ Apache ZooKeeper is an effort to de ...
- 2015306 白皎 《网络攻防》Exp4 恶意代码分析
2015306 白皎 <网络攻防>Exp4 恶意代码分析 netstat [Mac.Linux.Win] sysinteral [MS]:1 2 3 一.系统监控--Windows计划任务 ...
- @ModelAttribute三个作用:
@ModelAttribute具有如下三个作用: ①绑定请求参数到命令对象:放在功能处理方法的入参上时,用于将多个请求参数绑定到一个命令对象,从而简化绑定流程,而且自动暴露为模型数据用于视图页面展示时 ...
- UOJ UR#9 App管理器
题目传送门 题目大意大概就是给你一个混合图(既有有向边又有无向边),对于每条无向边,u-v,问删去u->v,或删去v->u那条可以使新图强连通.(保证数据有解). 这道题前几个数据点送分. ...
- Hall定理
Hall定理 Tags:图论 zybl 二分图\(G=<V1,V2,E>\)中,\(|V1|<|V2|\),当且仅当\(V1\)中任意\(k(=1,2,3..|V1|)\)个顶点都与 ...
- 1 nodejs
重点 :
- 团队合作开发git冲突解决方案 Intellij IDEA
https://blog.csdn.net/antony9118/article/details/52524873 注:因为我使用的是 idea 2018.1.2的版本,所以在操作上有些变化 将某些操 ...
- flag -- 诡异的memcache标记
引子 打从去年一路北漂,进入无人货架行业,业务需求漫天飘,最近总算把工作都规划齐整.回望过去一年多的时间里,诸多东西值得整理,memcache就是其中一个. 看到java的工资高些,队伍中好 ...
- 「专题训练」游走(BZOJ-3143)
题意与分析 定义走到每条边的期望为\(e_i\),一开始的想法是给定一个\(\large\sum_{i=1}^n e_i a_i\),求一个a的排列使得这个和最小.问题在于这样等于没对题目作分析,而且 ...
- elementUI el-select 多选情况下包含全部选项,及获得选中项的label
<template> <div> <span style="margin-left:30px;font-weight:bolder;">教练: ...