java post request code

/*
 * Project Name:Tconfig
 * File Name:Main.java
 * Package Name:packages
 * Date:2017-3-21下午6:03:01
 * Copyright (c) 2017, [email protected] 陈飞  Rights Reserved.
 *
 */
package packages;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

import common.Common;
import data.S1;

/*
 * ClassName:Main
 * Function:   ADD FUNCTION.
 * Reason:     ADD REASON.
 * Date:     2017-3-21 下午6:03:01
 * @author   guosen
 * @version
 * @since    JDK 1.6
 */
public class Main {

    public static void main(String[] args) throws InterruptedException,IOException {

        //接口地址
        String urlStr = "http://172.24.175.81:8081/WebServices/Seal.ashx";

        URL url = new URL(urlStr);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setRequestMethod("POST"); // POST方法
        conn.connect();

        // 写入的POST数据
        OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream(),"UTF-8");
        osw.write(S1.jsonString);
        osw.flush();
        osw.close();

        // 读取响应数据
        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String s;
        while ((s = in.readLine()) != null)
            System.out.println(s);

        /*
         * while(true) { System.out.println(new Common().getsecnum());
         * Thread.sleep(1000); }
         */

    }
}
时间: 2024-11-07 22:22:33

java post request code的相关文章

android模拟器 报错:X Error of failed request: BadRequest (invalid request code or no such operation)

最近在ubuntu12.04上学习python,python2.7 python3.2差别还是蛮大的,就想着学习较新的 升级后 结果出现 输入法不显示   update-manager 和  add-apt-repository不能用,android的模拟器也打不开. 罪魁祸首直至python 因为python3.2没有往上兼容,导致从python2.7升级到3.2 很多依赖之前2.7版本的功能无法使用 如何解决python升级的问题 请 http://book.51cto.com/art/20

java 获取request和response的一种方法

java获取request和response: HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();//获取response  HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttrib

ajax传JSON时设置的contenttype导致JAVA中request.getParameter("")怎么也接收不到数据

ajax传JSON时设置的contenttype默认值是application/x-www-form-urlencoded, 当ajax传JSON时设置的contenttype 如果是application/json或者text/json时,JAVA中request.getParameter("")是接收不到数据的. 因为在java后台会认为你传递的是一个纯文本流,而非键值对,是不能存入requestMap的. 可以把流读取出来转成json转成lsit或对象等,在Spring中使用@R

java读取request中的xml

java读取request中的xml 答: // 读取xml InputStream inputStream; StringBuffer sb = new StringBuffer(); inputStream = request.getInputStream(); String s; BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")); while ((s = in.r

[Java][Web]Request 实现转发和 MVC 设计模式

String data = "aaaaa"; request.setAttribute("data",data); // 将数据存在 request request.getRequestDispatcher("/message.jsp").forward(request,response); // 实现转发 MVC => M model/javabean  V view/jsp  C controller / servlet 在 messa

JAVA Web request对象

request对象 request对象用于处理HTTP中请求的各项参数,最常用的就是获取请求参数.如果要同时指定多个参数,各参数间使用与(&)符合分隔即可 例通过request对象的getParameter()方法获取传递的参数值代码如下 //index页面用于连接到delta <%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB1

读代码学java:request.getParameterNames

1 protected Map<String, String> initRequestParams(HttpServletRequest request) { 2 Map<String, String> paramMap = new HashMap<String, String>(); 3 if (request == null) { 4 return paramMap; 5 } 6 Enumeration<?> paramNames = request.g

java修改request的paramMap

最近做项目,发现要修改request的参数内容.因为想要在request的paramMap里面默认注入,modifier和modifierName,这些内容.但是这个Map是不能修改的.所以采用了如下办法:下面的办法网上也有的这里只做备份. 1. 创建request转换器 package com.tmall.lafite.web.base; import java.util.Enumeration; import java.util.Map; import java.util.Vector; i

java获取request的头信息

1.获取全部头信息: //get request headers private Map<String, String> getHeadersInfo() { Map<String, String> map = new HashMap<String, String>(); Enumeration headerNames = request.getHeaderNames(); while (headerNames.hasMoreElements()) { String k