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

Servlet发送Http请求

时间:2015-07-08 12:33:12      阅读:912      评论:0      收藏:0      [点我收藏+]

标签:

今日遇到一个需求,android注册,短信验证码功能。

android请求我服务端,我请求tosms.cn发送验证码短信给android,于是需要在Servlet中发送Http请求

package org.helloword;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpInvoker {

    public static String STR_URL = "http://localhost:8080/JsonProject/servlet/JsonServlet?action_flag=person";

    public static void readContentFromGet() throws IOException {
        STR_URL = STR_URL + "&param=paramstr";
        URL getUrl = new URL(STR_URL);
        HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
        connection.connect();
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                connection.getInputStream()));
        System.out.println("=============get================");
        String lines;
        while ((lines = reader.readLine()) != null) {
            System.out.println(lines);
        }
        reader.close();
        connection.disconnect();
    }

    public static void readContentFromPost() throws IOException {
        URL postUrl = new URL(STR_URL);
        HttpURLConnection connection = (HttpURLConnection) postUrl
                .openConnection();
        connection.setDoOutput(true); //post这个地方设置为true
        connection.setDoInput(true);
        connection.setRequestMethod("POST");// Post 请求不能使用缓存
        connection.setUseCaches(false);// URLConnection.setInstanceFollowRedirects是成员函数,仅作用于当前函数
        connection.setInstanceFollowRedirects(true);// 配置本次连接的Content-type,配置为application/x-www-form-urlencoded的
        // 意思是正文是urlencoded编码过的form参数,下面我们可以看到我们对正文内容使用URLEncoder.encode
        // 进行编码
        connection.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded");
        // 连接,从postUrl.openConnection()至此的配置必须要在connect之前完成,
        // 要注意的是connection.getOutputStream会隐式的进行connect。
        connection.connect();
        DataOutputStream out = new DataOutputStream(connection
                .getOutputStream());

        out.flush();
        out.close(); 
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                connection.getInputStream()));
        String line;
        System.out.println("=============post================");
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }
        reader.close();
        connection.disconnect();
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        try {
            readContentFromGet();
            readContentFromPost();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //乱码用New String(xx.getByte("iso-8859-1"),"utf-8")解决

}

 

源代码http://down.51cto.com/data/2066142

Servlet发送Http请求

标签:

原文地址:http://www.cnblogs.com/xiaoliu66007/p/4629754.html

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