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();
    }
    }
    }

  • 相关阅读:
    GitHub统计
    不错的第三方控件
    仿射变换(CGAffineTransform)使用小结
    AffineTransform(仿射变换)
    使用CAShapeLayer实现复杂的View的遮罩效果
    使用CAShapeLayer实现一个音量大小动态改变的控件
    window10 Docker仓库访问
    postgresql从timestamp(6)复制到timestamp(0),时间会变
    在编译Dll文件的时候遇到dll 链接不一致的问题
    qtquery 取列的值
  • 原文地址:https://www.cnblogs.com/feng666666/p/11081375.html
Copyright © 2011-2022 走看看