zoukankan      html  css  js  c++  java
  • java http get post

    package com.szseq.restwebservice;

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.net.URL;
    import java.net.URLConnection;

    public class HttpUtil
    {
    public String httpget(String url, String Authorization, String LogPath)
    {
    Log log = new Log();
    URLConnection connection = null;
    String aa = null;
    try {
    connection = new URL(url).openConnection();
    connection.setRequestProperty("User-Agent", "Fidder");
    connection.setRequestProperty("Authorization", Authorization);
    connection.setConnectTimeout(5000);
    connection.connect();
    InputStream fin = connection.getInputStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(fin, "utf-8"));
    StringBuffer buffer = new StringBuffer();
    String temp = null;
    while ((temp = br.readLine()) != null) {
    buffer.append(temp);
    }
    aa = buffer.toString();
    } catch (IOException e) {
    e.printStackTrace();
    log.saveLog(e, LogPath);
    }
    return aa;
    }

    public String httppost(String url, String Authorization, String param, String LogPath) {
    Log log = new Log();
    OutputStreamWriter out = null;
    URLConnection connection = null;
    String aa = null;
    try {
    connection = new URL(url).openConnection();
    connection.setRequestProperty("User-Agent", "Fidder");
    connection.setRequestProperty("Authorization", Authorization);
    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    connection.setConnectTimeout(5000);
    connection.setDoOutput(true);
    connection.setDoInput(true);
    out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
    out.write(param);
    out.flush();
    out.close();
    InputStream fin = connection.getInputStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(fin, "utf-8"));
    StringBuffer buffer = new StringBuffer();
    String temp = null;
    while ((temp = br.readLine()) != null) {
    buffer.append(temp);
    }
    aa = buffer.toString();
    } catch (IOException e) {
    e.printStackTrace();
    log.saveLog(e, LogPath);
    }
    return aa;
    }
    }

    package com.szseq.restwebservice;

    import java.io.File;
    import java.io.FileWriter;
    import java.io.PrintWriter;
    import java.text.SimpleDateFormat;
    import java.util.Date;

    public class Log
    {
    private File file = null;

    public File getFile() {
    return this.file;
    }

    public void setFile(File file) {
    this.file = file;
    }

    public void saveLog(Exception e, String path) {
    try {
    this.file = new File(path);
    PrintWriter writer = null;
    FileWriter fileWrite = new FileWriter(this.file, true);
    writer = new PrintWriter(fileWrite);
    writer.append(System.getProperty("line.separator") +
    new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss").format(new Date()));
    writer.append(System.getProperty("line.separator"));
    writer.append(" *************************" + e.toString() + "*************************");
    writer.append(System.getProperty("line.separator"));
    e.printStackTrace(writer);
    writer.flush();
    writer.close();
    } catch (Exception e2) {
    e2.printStackTrace();
    }
    }
    }

  • 相关阅读:
    MyBatis代码自动生成
    英语单词--程序员专属
    动态规划小例子
    Dijkstra 算法
    矩阵变换
    (转)贝塞尔曲线
    山寨了@上位者的怜悯的样式= =
    HDU 1115 (计算多边形重心)
    HDU 1147(线段相交+链表)
    HDU 1276 (直接链表模拟)
  • 原文地址:https://www.cnblogs.com/feng666666/p/11081375.html
Copyright © 2011-2022 走看看