代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
socket实现http POST EGT
作者:
wangjinxi7758
/ 发布于
2015/2/15
/
518
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.InetSocketAddress; import java.net.Socket; import java.net.SocketAddress; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.List; public class TestSocketLogin { public static final String socketIp = "*.*.*.*"; public static final int socketPort = 8080; private Socket socket; public static void main(String[] args) throws Exception { TestSocketLogin mylogin = new TestSocketLogin(); // List<String> list = mylogin.send("GET","https://www.oschina.net/home/login",null); String URL = "https://www.oschina.net/action/user/hash_login"; String postStr = "email="+"***@qq.com"+"&pwd="+SHA1("***")+"&verifyCode=&save_login=1&"; List<String> list = mylogin.send("POST",URL,postStr); //List<String> list = mylogin.send("POST",URL,postStr); System.out.println(list.get(2)); } public TestSocketLogin(){ Socket socket = new Socket(); this.socket = socket; } public static String SHA1(String decript) throws Exception { MessageDigest digest = java.security.MessageDigest.getInstance("SHA-1"); digest.update(decript.getBytes()); byte messageDigest[] = digest.digest(); // Create Hex String StringBuffer hexString = new StringBuffer(); // 字节数组转换为 十六进制 数 for (int i = 0; i < messageDigest.length; i++) { String shaHex = Integer.toHexString(messageDigest[i] & 0xFF); if (shaHex.length() < 2) { hexString.append(0); } hexString.append(shaHex); } return hexString.toString(); } private List<String> send(String method,String URL,String postStr) throws Exception{ OutputStream out = getSocketStream(); out.write(sendMethod(method, URL, postStr)); BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8")); String line = ""; String head = ""; String oscid = ""; int contentLength = 0; while ((line = br.readLine()) != null) { if (line.startsWith("Content-Length")) { contentLength = Integer.parseInt(line.substring(line.indexOf(':') + 1).trim()); } head += line +"\r\n"; if(line.contains("oscid=")) oscid = line.split("=")[1].split(";")[0]; if (line.equals("")) break; } System.out.println("----返回 head: ------\r\n" + head); // 显示 POST 表单提交的内容, 这个内容位于请求的主体部分 String body = ""; if (contentLength > 0) { System.out.println("返回包体内容:"); for (int i = 0; i < contentLength; i++) { body += (char) br.read(); System.out.print((char) br.read()); } } List<String> list = new ArrayList<String>(); list.add(head); list.add(body); list.add(oscid); out.close(); br.close(); socket.close(); return list; } private OutputStream getSocketStream() throws Exception{ SocketAddress dest = new InetSocketAddress(socketIp, socketPort); socket.connect(dest); OutputStream out = socket.getOutputStream(); return out; } private byte[] sendMethod(String method,String URL,String postStr){ StringBuffer sb = new StringBuffer(method.toUpperCase()+" "+URL+" HTTP/1.1\r\n"); sb.append("Accept: */*\r\n"); sb.append("Accept-Encoding: gzip, deflate\r\n"); sb.append("Accept-Language: zh-CN,zh;q=0.8\r\n"); sb.append("Connection: keep-alive\r\n"); sb.append("Content-Type: application/x-www-form-urlencoded\r\n"); //sb.append("Cookie: _reg_key_=XB8MajstLi3QmrN2trsb\r\n"); sb.append("X-Requested-With: XMLHttpRequest\r\n"); if("POST".equalsIgnoreCase(method)) sb.append("Content-Length: "+postStr.length()+"\r\n"); /*sb.append("Host: 443\r\n");*/ sb.append("\r\n"); if("POST".equalsIgnoreCase(method)) sb.append(postStr); return sb.toString().getBytes(); } }
试试其它关键字
POST
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
wangjinxi7758
贡献的其它代码
(
1
)
.
socket实现http POST EGT
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3