码迷,mamicode.com
首页 > Web开发 > 详细

HttpUrlconnection POST提交 用于接口调用传参

时间:2015-06-28 17:38:45      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:接口   post提交   调用   

package com.jr.demo;

import java.io.BufferedReader;

import java.io.DataOutputStream;
import java.io.IOException;

import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;


public class C {

    public static void main(String[] args) throws IOException {
        URL url=new URL("http://pc201407240141:8080/Spring_MVC_Test/loging.html");
        HttpURLConnection connection=(HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setUseCaches(false);
         connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
        connection.connect();
        DataOutputStream outputStream=new DataOutputStream(connection.getOutputStream());
        String content="name="+URLEncoder.encode("吕佳瑞", "gbk");
        content+="&pwd="+URLEncoder.encode("123", "gbk");
        outputStream.writeBytes(content);
        outputStream.flush();
        outputStream.close();

        InputStreamReader is=new InputStreamReader(connection.getInputStream());

        BufferedReader br=new BufferedReader(is);
        String line="";
        StringBuffer sb=new StringBuffer();
        while ((line=br.readLine())!=null) {
            sb.append(line);
        }

        System.out.println(sb.toString());
        br.close();

    }

}

HttpUrlconnection POST提交 用于接口调用传参

标签:接口   post提交   调用   

原文地址:http://blog.csdn.net/f5912766/article/details/46671369

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!