`

JAVA实现post和get请求

    博客分类:
  • JAVA
阅读更多
使用Java发送post请求
import java.net.*;
import java.io.*;
import java.util.Locale;
class PostTest
{
    public static void main(String[] args)
    {
        try
        {
            //用于注册的用户名 密码 问题 答案临时变量
            //String temp="tester";
                String urlstr="http://localhost/postTest/register.php";
                URL url=new URL(urlstr);
                URLConnection conn=url.openConnection();
                //这里是关键,表示我们要向链接里输出内容
                conn.setDoOutput(true);
                //获得连接输出流
                OutputStreamWriter out=new OutputStreamWriter(conn.getOutputStream());
                //这里是我定义了一组账号信息,字段+数据
                String str=String.format(Locale.CHINA,"name=%s&&pwd=%s",
                    "admin","123");
                //把数据写入
                out.write(str);
                out.flush();
                out.close();
                //到这里已经完成了,不过我们还是看看返回信息吧,他的注册返回信息也在此页面
                BufferedReader reader=new BufferedReader(new InputStreamReader(conn.getInputStream()));
                String line=null;
                int lineNum=1;
                while((line=reader.readLine())!=null)
                {
                    ++lineNum;
                    System.out.println(line);
                }
        }
        catch (Exception x)
        {
            System.out.println(x.toString());
        }
    }
}
 
   
用java发送get请求

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
/**
 *
 * @author Administrator
 */
public class Createulr {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws UnsupportedEncodingException, IOException{
        String  app_id = "123";
        String goods_id = "1";
        String user_id = "56908";//56908/11167810;
        String num = "10";
        String url = "";
        String sign = "";
        String key = "pineNut_liugc";
        md5 md5 = new md5();
        String str = "app_id="+app_id+"&goods_id="+goods_id+"&num="+num+"&user_id="+user_id+key;
        //System.out.println(str);
        sign = md5.getMD5Str(str).toUpperCase();
        url = "
http://uctest3.ucweb.com:7801/pineNuts/pineNuts/api/query.php?app_id="+app_id+"&goods_id="+goods_id+"&user_id="+user_id+"&num="+num+"&sign="+sign;
        getUrlContent(url);
    }
     /**
         * 读取URL指定的网页内容
         *
         * @throws IOException
         */
        public static void getUrlContent(String link) throws IOException {
                URL url = new URL(link);
                //打开到此 URL 的连接并返回一个用于从该连接读入的 InputStream。
                Reader reader = new InputStreamReader(new BufferedInputStream(url.openStream()));
                int c;
                while ((c = reader.read()) != -1) {
                        System.out.print((char) c);
                }
                reader.close();
        }

}
无联机状态信息       
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics