📄 mhttpc.java
字号:
package net.jumperz.app.MHttpC;
import net.jumperz.gui.*;
import java.io.*;
import java.net.*;
import java.util.*;
import org.eclipse.swt.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.custom.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.events.*;
public class MHttpC
implements Listener, ModifyListener, SelectionListener
{
private static MHttpC instance;
protected static final String appName = "MHttpC@JUMPERZ.NET";
private static final String defaultHeight = "320";
private static final String defaultWidth = "500";
private static final String defaultTop = "100";
private static final String defaultLeft = "100";
private static final String defaultWeight1 = "350";
private static final String defaultWeight2 = "650";
private static final int STATE_DISCONNECTED = 0;
private static final int STATE_CONNECTING = 1;
private static final int STATE_CONNECTED = 2;
private Composite composite1;
private SashForm sashForm;
private Button button1, button2, button3;
private Text text1, text2, text3;
private FormData label1Data;
private FormData text1Data, text2Data, text3Data;
private FormData button1Data;
private FormData button2Data;
private FormData button3Data;
private Label label1;
private MSWTSyncUtil swtUtil;
private int height;
private int width;
private int top;
private int left;
private int weight1;
private int weight2;
private File configFile;
protected Properties prop;
protected Shell shell;
private int state = STATE_DISCONNECTED;
private boolean initialized = false;
private boolean isHttp = true;
private String host;
private int port;
private URL url;
//-----------------------------------------------------------------------------------
public static void main( String[] args )
throws Exception
{
if( args.length == 1 )
{
instance = new MHttpC( args[ 0 ] );
instance.startShell();
}
else
{
System.out.println( "java app [ config_file ]" );
return;
}
}
//-----------------------------------------------------------------------------------
public void startShell()
throws Exception
{
Display display = new Display();
shell = new Shell( display );
startApplication();
shell.setText( appName );
shell.setSize( width, height );
shell.setLocation( left, top );
//shell.pack();
shell.open();
shell.addListener( SWT.Close , this );
shell.addListener( SWT.Resize , this );
while (!shell.isDisposed())
{
if ( !display.readAndDispatch() )
{
display.sleep();
}
}
display.dispose();
}
//-----------------------------------------------------------------------------------
private void loadConfig()
throws IOException
{
prop = new Properties();
if( configFile.exists() )
{
FileInputStream inifile_in = new FileInputStream( configFile );
prop.load( inifile_in );
}
height = Integer.parseInt( prop.getProperty( "height" , defaultHeight ) );
width = Integer.parseInt( prop.getProperty( "width" , defaultWidth ) );
top = Integer.parseInt( prop.getProperty( "top" , defaultTop ) );
left = Integer.parseInt( prop.getProperty( "left" , defaultLeft ) );
weight1 = Integer.parseInt( prop.getProperty( "weight1" , defaultWeight1 ) );
weight2 = Integer.parseInt( prop.getProperty( "weight2" , defaultWeight2 ) );
}
//--------------------------------------------------------------------------------
public MHttpC( String arg )
throws IOException
{
configFile = new File( arg );
loadConfig();
}
//--------------------------------------------------------------------------------
protected void startApplication()
throws Exception
{
shell.setLayout( new FillLayout() );
sashForm = new SashForm( shell, SWT.HORIZONTAL );
composite1 = new Composite( sashForm, SWT.NULL );
text3 = new Text( sashForm,SWT.MULTI|SWT.BORDER|SWT.H_SCROLL|SWT.V_SCROLL );
text3.addListener( SWT.Resize , this );
sashForm.setWeights( new int[]{ weight1, weight2 } );
FormLayout fl = new FormLayout();
fl.marginHeight = 0;
fl.marginWidth = 0;
composite1.setLayout( fl );
composite1.addListener( SWT.Resize , this );
label1Data = new FormData();
label1Data.left = new FormAttachment( 0, 10 );
label1Data.top = new FormAttachment( 0, 10 );
label1 = new Label( composite1, SWT.NULL );
label1.setText( "URL:" );
label1.setLayoutData( label1Data );
text1Data = new FormData();
text1Data.right = new FormAttachment( 100, -13 );
text1Data.left = new FormAttachment( label1, 0, SWT.RIGHT );
text1Data.top = new FormAttachment( 0, 6 );
text1 = new Text( composite1, SWT.BORDER );
text1.setLayoutData( text1Data );
text1.addModifyListener( this );
button1Data = new FormData();
button1Data.left = new FormAttachment( 0, 13 );
button1Data.top = new FormAttachment( 0, 30 );
button1 = new Button( composite1, SWT.PUSH );
button1.setText( "Generate Request" );
button1.setLayoutData( button1Data );
button1.setEnabled( false );
button1.addSelectionListener( this );
text2Data = new FormData();
text2Data.left = new FormAttachment( 0, 13 );
text2Data.top = new FormAttachment( 0, 60 );
text2Data.right = new FormAttachment( 100, -13 );
text2Data.bottom = new FormAttachment( 100, -40 );
text2 = new Text( composite1, SWT.MULTI|SWT.BORDER|SWT.H_SCROLL|SWT.V_SCROLL );
text2.setLayoutData( text2Data );
button2Data = new FormData();
button2Data.top = new FormAttachment( text2, 10, SWT.BOTTOM );
button2Data.left = new FormAttachment( 0, 13 );
button2Data.width = 80;
Button button2 = new Button( composite1, SWT.PUSH );
button2.setText( "Execute" );
button2.setLayoutData( button2Data );
button2.setEnabled( false );
button3Data = new FormData();
button3Data.top = new FormAttachment( text2, 10, SWT.BOTTOM );
button3Data.left = new FormAttachment( button2, 6, SWT.RIGHT );
button3Data.width = 80;
Button button3 = new Button( composite1, SWT.PUSH );
button3.setText( "Disconnect" );
button3.setLayoutData( button3Data );
button3.setEnabled( false );
initialized = true;
}
//-----------------------------------------------------------------------------------
public void handleEvent( Event event )
{
try
{
if( event.widget == composite1 )
{
if( event.type == SWT.Resize )
{
onComposite1Resize();
}
}
else if( event.widget == text3 )
{
if( event.type == SWT.Resize )
{
onText3Resize();
}
}
else if( event.widget == shell )
{
switch( event.type )
{
case SWT.Close:
this.onClose();
break;
case SWT.Resize:
this.onResize();
break;
}
}
}
catch( Exception e )
{
System.out.println( event );
e.printStackTrace();
}
}
//-----------------------------------------------------------------------------------
private void saveConfig()
throws IOException
{
Point size = shell.getSize();
prop.setProperty( "width" , String.valueOf( size.x ) );
prop.setProperty( "height" , String.valueOf( size.y ) );
Point location = shell.getLocation();
prop.setProperty( "top" , String.valueOf( location.y ) );
prop.setProperty( "left" , String.valueOf( location.x ) );
prop.setProperty( "weight1", Integer.toString( weight1 ) );
prop.setProperty( "weight2", Integer.toString( weight2 ) );
FileOutputStream configFileOut = new FileOutputStream( configFile );
prop.store( configFileOut, "" );
}
//-----------------------------------------------------------------------------------
protected void onText3Resize()
throws Exception
{
}
//-----------------------------------------------------------------------------------
protected void onResize()
throws Exception
{
}
//-----------------------------------------------------------------------------------
protected void onClose()
throws Exception
{
saveConfig();
}
//-----------------------------------------------------------------------------------
protected void onComposite1Resize()
throws Exception
{
weight1 = sashForm.getWeights()[ 0 ];
weight2 = sashForm.getWeights()[ 1 ];
}
//--------------------------------------------------------------------------------
public void syncGui()
{
swtUtil = new MSWTSyncUtil()
{
public void updateSWT()
{
updateGui();
}
};
Display.getCurrent().asyncExec( swtUtil );
}
//--------------------------------------------------------------------------------
public void updateGui()
{
if( !initialized )
{
return;
}
boolean button1Enabled = false;
String uri = text1.getText();
if( ( uri.indexOf( "http://" ) == 0 && uri.length() > 7 )
|| ( uri.indexOf( "https://" ) == 0 && uri.length() > 8 )
)
{
button1Enabled = true;
}
button1.setEnabled( button1Enabled );
}
//--------------------------------------------------------------------------------
public void modifyText( ModifyEvent e )
{
if( e.widget == text1 )
{
text1Modify();
}
}
//--------------------------------------------------------------------------------
private void text1Modify()
{
updateGui();
}
//--------------------------------------------------------------------------------
private void onButton1Click()
throws IOException
{
String requestPath = null;
int protocolLength = 0;
url = new URL( text1.getText() );
if( url.getProtocol().equals( "http" ) )
{
protocolLength = 7;
isHttp = true;
}
else if( url.getProtocol().equals( "https" ) )
{
protocolLength = 8;
isHttp = false;
}
else
{
return;
}
host = url.getHost();
port = url.getPort();
if( port == -1 )
{
if( isHttp )
{
port = 80;
}
else
{
port = 443;
}
}
String urlStr = text1.getText();
urlStr = urlStr.substring( protocolLength );
int index = urlStr.indexOf( "/" );
if( index == -1 )
{
urlStr = "/";
}
else
{
urlStr = urlStr.substring( index );
}
StringBuffer s = new StringBuffer();
s.append( "GET " );
s.append( urlStr );
s.append( " HTTP/1.0" );
s.append( "\r\n" );
s.append( "Host: " );
s.append( host );
if( isHttp && port != 80 )
{
s.append( ":" + port );
}
else if( !isHttp && port != 443 )
{
s.append( ":" + port );
}
s.append( "\r\n\r\n" );
text2.setText( s.toString() );
}
//--------------------------------------------------------------------------------
public void widgetSelected( SelectionEvent arg0 )
{
try
{
if( arg0.getSource() == button1 )
{
onButton1Click();
}
}
catch( Exception e )
{
e.printStackTrace();
}
}
//--------------------------------------------------------------------------------
public void widgetDefaultSelected( SelectionEvent arg0 )
{
System.err.println( arg0 );
}
//--------------------------------------------------------------------------------
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -