Commit fb2d11ec authored by 9731301's avatar 9731301

fix content-type and and support multupart form data

parent 87963623
......@@ -7,23 +7,9 @@
</component>
<component name="ChangeListManager">
<list default="true" id="f2dd20fb-7e97-49ef-9dfe-5631626d7912" name="Default Changelist" comment="">
<change afterPath="$PROJECT_DIR$/Requests/test2.insomnia" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Requests/test.insomnia" beforeDir="false" afterPath="$PROJECT_DIR$/Requests/test.insomnia" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Requests/test2.insomnia" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/GUI/InsomniaGUI$1.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/GUI/InsomniaGUI$1.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/GUI/InsomniaGUI$2.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/GUI/InsomniaGUI$2.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/GUI/InsomniaGUI$3.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/GUI/InsomniaGUI$3.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/GUI/InsomniaGUI$4.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/GUI/InsomniaGUI$4.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/GUI/InsomniaGUI$5.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/GUI/InsomniaGUI$5.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/GUI/InsomniaGUI$6.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/GUI/InsomniaGUI$6.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/GUI/InsomniaGUI$exitApp$1.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/GUI/InsomniaGUI$exitApp$1.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/GUI/InsomniaGUI$exitApp$2.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/GUI/InsomniaGUI$exitApp$2.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/GUI/InsomniaGUI$exitApp.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/GUI/InsomniaGUI$exitApp.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/GUI/InsomniaGUI.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/GUI/InsomniaGUI.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/GUI/LeftPanel$1$1.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/GUI/LeftPanel$1$1.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/GUI/LeftPanel$1.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/GUI/LeftPanel$1.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/GUI/LeftPanel$MouseRequestClick.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/GUI/LeftPanel$MouseRequestClick.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/GUI/LeftPanel.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/GUI/LeftPanel.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/GUI/RequestPanel$1.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/GUI/RequestPanel$1.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/GUI/RequestPanel$10.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/GUI/RequestPanel$10.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/GUI/RequestPanel$11.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/GUI/RequestPanel$11.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/GUI/RequestPanel$12.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/GUI/RequestPanel$12.class" afterDir="false" />
......@@ -37,15 +23,12 @@
<change beforePath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/GUI/RequestPanel$8.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/GUI/RequestPanel$8.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/GUI/RequestPanel$9.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/GUI/RequestPanel$9.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/GUI/RequestPanel.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/GUI/RequestPanel.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/parser/RequestData.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/parser/RequestData.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/parser/RequestLoader.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/parser/RequestLoader.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/parser/ResponseData.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/parser/ResponseData.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/parser/Saver.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/Insomnia/com/insomnia/parser/Saver.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/com/insomnia/GUI/InsomniaGUI.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/com/insomnia/GUI/InsomniaGUI.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/com/insomnia/GUI/LeftPanel.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/com/insomnia/GUI/LeftPanel.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/com/insomnia/GUI/RequestPanel.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/com/insomnia/GUI/RequestPanel.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/com/insomnia/parser/RequestData.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/com/insomnia/parser/RequestData.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/com/insomnia/parser/RequestLoader.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/com/insomnia/parser/RequestLoader.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/com/insomnia/parser/ResponseData.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/com/insomnia/parser/ResponseData.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/com/insomnia/parser/Saver.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/com/insomnia/parser/Saver.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
......@@ -181,7 +164,9 @@
<workItem from="1590757579057" duration="2543000" />
<workItem from="1590934179600" duration="31544000" />
<workItem from="1591024948574" duration="21598000" />
<workItem from="1591127468443" duration="7799000" />
<workItem from="1591127468443" duration="7941000" />
<workItem from="1591179932347" duration="2513000" />
<workItem from="1591205535989" duration="7908000" />
</task>
<servers />
</component>
......@@ -216,30 +201,30 @@
<screen x="0" y="0" width="1536" height="824" />
</state>
<state x="402" y="167" key="FileChooserDialogImpl/0.0.1536.824@0.0.1536.824" timestamp="1590494460700" />
<state width="1493" height="105" key="GridCell.Tab.0.bottom" timestamp="1591135156379">
<state width="1493" height="154" key="GridCell.Tab.0.bottom" timestamp="1591213140323">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state width="1493" height="105" key="GridCell.Tab.0.bottom/0.0.1536.824@0.0.1536.824" timestamp="1591135156379" />
<state width="1493" height="105" key="GridCell.Tab.0.center" timestamp="1591135156379">
<state width="1493" height="154" key="GridCell.Tab.0.bottom/0.0.1536.824@0.0.1536.824" timestamp="1591213140323" />
<state width="1493" height="154" key="GridCell.Tab.0.center" timestamp="1591213140322">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state width="1493" height="105" key="GridCell.Tab.0.center/0.0.1536.824@0.0.1536.824" timestamp="1591135156379" />
<state width="1493" height="105" key="GridCell.Tab.0.left" timestamp="1591135156379">
<state width="1493" height="154" key="GridCell.Tab.0.center/0.0.1536.824@0.0.1536.824" timestamp="1591213140322" />
<state width="1493" height="154" key="GridCell.Tab.0.left" timestamp="1591213140322">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state width="1493" height="105" key="GridCell.Tab.0.left/0.0.1536.824@0.0.1536.824" timestamp="1591135156379" />
<state width="1493" height="105" key="GridCell.Tab.0.right" timestamp="1591135156379">
<state width="1493" height="154" key="GridCell.Tab.0.left/0.0.1536.824@0.0.1536.824" timestamp="1591213140322" />
<state width="1493" height="154" key="GridCell.Tab.0.right" timestamp="1591213140323">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state width="1493" height="105" key="GridCell.Tab.0.right/0.0.1536.824@0.0.1536.824" timestamp="1591135156379" />
<state width="1493" height="154" key="GridCell.Tab.0.right/0.0.1536.824@0.0.1536.824" timestamp="1591213140323" />
<state x="249" y="0" key="SettingsEditor" timestamp="1590962002174">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state x="249" y="0" key="SettingsEditor/0.0.1536.824@0.0.1536.824" timestamp="1590962002174" />
<state x="425" y="237" key="com.intellij.ide.util.TipDialog" timestamp="1591127805670">
<state x="425" y="237" key="com.intellij.ide.util.TipDialog" timestamp="1591205543692">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state x="425" y="237" key="com.intellij.ide.util.TipDialog/0.0.1536.824@0.0.1536.824" timestamp="1591127805670" />
<state x="425" y="237" key="com.intellij.ide.util.TipDialog/0.0.1536.824@0.0.1536.824" timestamp="1591205543692" />
<state x="499" y="179" key="extract.method.dialog" timestamp="1590145349556">
<screen x="0" y="0" width="1536" height="824" />
</state>
......
......@@ -11,11 +11,12 @@ import java.awt.image.BufferedImage;
import java.io.DataInput;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
public class RequestPanel extends JPanel {
private VerticalFlowLayout vfLayout = new VerticalFlowLayout();
private String[] ss = {"Form Data","JSON","Binary Data"};
private String[] ss = {"Form","Multipart Form Data","JSON","Binary Data"};
private JComboBox Json_FormData = new JComboBox(ss);
private JPanel body_pane = new JPanel(){
@Override
......@@ -176,6 +177,22 @@ public class RequestPanel extends JPanel {
return headers;
}
public void addContentType(String type){
for(Component c : mainCenterHeader.getComponents()){
if (c instanceof JPanel){
JPanel nh = (JPanel) c;
JPanel l = (JPanel) nh.getComponent(1);
JTextField h = (JTextField) l.getComponent(0);
JTextField v = (JTextField) l.getComponent(1);
if (h.getText().toLowerCase().trim().equals("content-type")){
v.setText(type);
return;
}
}
}
addHeader("Content-Type",type);
}
public HashMap<String,String> getFormDataBody(){
HashMap<String,String> bodydata = new HashMap<>();
for(Component c : ((JPanel)body_pane.getComponent(0)).getComponents()){
......@@ -197,11 +214,31 @@ public class RequestPanel extends JPanel {
if(b.size()==0) return "";
String f = "";
for (String key : b.keySet()){
if (key=="-file") continue;
f = f+key+"="+b.get(key)+"&";
}
return f.substring(0,f.length()-1);
}
public String findUploadPath(){
HashMap<String,String> b = getFormDataBody();
if(b.size()==0) return "";
String f = "";
for (String key : b.keySet()){
if (key=="-file") f = b.get(key);
}
return f;
}
public String getHeadersAsString(HashMap<String,String> b){
if(b.size()==0) return "";
String f = "";
for (String key : b.keySet()){
f = f+key+":"+b.get(key)+";";
}
return f.substring(0,f.length()-1);
}
int selectedItem = -2;
MouseAdapter body_insert = null;
void initBody(String... args){
......@@ -225,6 +262,7 @@ public class RequestPanel extends JPanel {
textArea.setBorder(border);
if (args.length>0) textArea.setText(args[0]);
body_pane.add(textArea);
addContentType("application/json");
} else if(Json_FormData.getSelectedItem().equals("Binary Data")){
JTextField path = new JTextField("File Path"){
@Override
......@@ -247,10 +285,11 @@ public class RequestPanel extends JPanel {
fc.setVisible(true);
String f = fc.getFile();
if (f!=null && !f.isEmpty()) {
path.setText(new File(fc.getDirectory(),fc.getName()).getAbsolutePath());
path.setText(new File(fc.getDirectory(),new File(f).getName()).getAbsolutePath());
}
}
});
addContentType("application/octet-stream");
}else {
final JPanel vl = new JPanel(vfLayout) {
......@@ -272,10 +311,18 @@ public class RequestPanel extends JPanel {
};
}
if (args.length==0) body_insert.mouseClicked(null);
if (Json_FormData.getSelectedItem().equals("Multipart Form Data")){
addContentType("multipart/form-data");
}else{
addContentType("application/x-www-form-urlencoded");
}
}
body_pane.revalidate();
body_pane.repaint();
}
private void addBody(String... args){
......@@ -331,30 +378,49 @@ public class RequestPanel extends JPanel {
sendButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
RequestData requestData = new RequestData();
requestData.setUrl(URLTextField.getText());
requestData.setMethod(Parser.findMethod((String)centerMenu.getSelectedItem()));
ArrayList<String> argsList = new ArrayList<>();
argsList.add("--url");
argsList.add(URLTextField.getText());
argsList.add("--Method");
argsList.add(Parser.findMethod((String)centerMenu.getSelectedItem()));
//set if it is Json or not
if(Json_FormData.getSelectedItem().equals("JSON")) {
requestData.setJson(true);
argsList.add("--json");
argsList.add("--data");
JTextArea textArea = (JTextArea) body_pane.getComponent(0);
requestData.setBody(textArea.getText().trim());
argsList.add(textArea.getText().trim());
}
else if(Json_FormData.getSelectedItem().equals("Binary Data")){
JTextField p = (JTextField) body_pane.getComponent(0);
if (!p.getText().isEmpty() && !p.getText().equals("File Path"))
requestData.setUploadPath(p.getText());
if (!p.getText().isEmpty() && !p.getText().equals("File Path")) {
argsList.add("--upload");
argsList.add(p.getText());
}
}else {
requestData.setBody(getFormDataBodyAsString());
argsList.add("--data");
argsList.add(getFormDataBodyAsString());
String upload = findUploadPath();
if (!upload.isEmpty() && !upload.equals("File Path")) {
argsList.add("--upload");
argsList.add(upload);
}
}
requestData.setHeaders(getHeaders());
requestData.setFollowRedirects(true);
requestData.setShowResponseHeaders(true);
requestData.setSaveRequest(false);
HashMap<String,String> map = getHeaders();
if (Json_FormData.getSelectedItem().equals("Multipart Form Data")){
map.put("Content-Type","multipart/form-data");
}
if (map!=null && map.size()>0){
argsList.add("--headers");
argsList.add(getHeadersAsString(map));
}
ResponseData data = RequestLoader.run(requestData);
argsList.add("-f");
String[] args = argsList.toArray(new String[argsList.size()]);
ResponseData data = RequestLoader.run(Parser.parse(args));
main_class.load(data);
}
});
......@@ -388,13 +454,13 @@ public class RequestPanel extends JPanel {
URLTextField.setText(r.getUrl());
if (r.isJson()){
Json_FormData.setSelectedIndex(1);
Json_FormData.setSelectedIndex(2);
initBody(r.getBody());
}else if(r.getUploadPath().length()>1){
Json_FormData.setSelectedIndex(2);
Json_FormData.setSelectedIndex(3);
initBody(r.getUploadPath());
}else{
Json_FormData.setSelectedIndex(0);
Json_FormData.setSelectedIndex(r.isMultipartFormData()?1:0);
initBody("r");
if (body_insert!=null &&!r.getBody().isEmpty()) {
if (!r.getBody().contains("&")) {
......
......@@ -25,6 +25,12 @@ public class RequestData implements Serializable {
private String body = "";
private String uploadPath = "";
boolean isMultipart = false;
public boolean isMultipartFormData() {
return isMultipart;
}
public String getName() {
return name;
}
......
package com.insomnia.parser;
import javax.net.ssl.HttpsURLConnection;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
......@@ -20,11 +21,6 @@ public class RequestLoader {
return responseData.finish();
}
if (data.isJson() ){
responseData.setSyntaxError(true);
responseData.setSyntaxErrorLog("enter a valid json data");
return responseData.finish();
}
File file = null;
......@@ -51,7 +47,7 @@ public class RequestLoader {
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setUseCaches(false);
if (data.getMethod().equals(RequestData.POST) && (!data.getBody().isEmpty()||!data.getUploadPath().isEmpty()) ){
if (!data.getMethod().equals(RequestData.GET) && (!data.getBody().isEmpty()||!data.getUploadPath().isEmpty()) ){
con.setDoOutput(true);
}
......@@ -59,26 +55,35 @@ public class RequestLoader {
con.setInstanceFollowRedirects(data.isFollowRedirects());
con.setRequestMethod(data.getMethod());
String content_type = "";
if (data.getHeaders()!=null){
for (String key : data.getHeaders().keySet()){
if (key.toLowerCase().equals("content-type")) {
content_type = data.getHeaders().get(key);
continue;
}
con.setRequestProperty(key,data.getHeaders().get(key));
}
}
if (data.isJson()){
con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
content_type = "application/json; charset=UTF-8";
}
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Cache-Control", "no-cache");
if (!data.getUploadPath().isEmpty()){
con.setRequestProperty(
"Content-Type", "multipart/form-data;boundary=" + boundary);
}
if (con.getDoOutput()){
if (content_type.toLowerCase().equals("multipart/form-data")) {
con.setRequestProperty(
"Content-Type", "multipart/form-data;boundary=" + boundary);
responseData.getRequestData().isMultipart = true;
}else{
con.setRequestProperty("Content-Type",content_type);
}
if (content_type.toLowerCase().equals("multipart/form-data") || !data.getUploadPath().isEmpty()) {
if (con.getDoOutput()){
if (!data.getUploadPath().isEmpty()){
DataOutputStream request = new DataOutputStream(con.getOutputStream());
DataOutputStream request = new DataOutputStream(con.getOutputStream());
String twoHyphens = "--";
String crlf = "\r\n";
if (!data.getBody().isEmpty()) {
......@@ -96,65 +101,71 @@ public class RequestLoader {
for (String key : fields.keySet()) {
request.writeBytes(twoHyphens + boundary + crlf);
request.writeBytes("Content-Disposition: form-data; name=\"" + key + "\""+ crlf);
request.writeBytes("Content-Disposition: form-data; name=\"" + key + "\"" + crlf);
request.writeBytes("Content-Type: text/plain; charset=UTF-8" + crlf);
request.writeBytes(crlf);
request.writeBytes(fields.get(key)+ crlf);
request.writeBytes(fields.get(key) + crlf);
request.flush();
}
}
String fieldName = "file";
String fileName = file.getName();
request.writeBytes(twoHyphens + boundary + crlf);
request.writeBytes("Content-Disposition: form-data; name=\"" +
fieldName + "\";filename=\"" +
fileName + "\"" + crlf);
request.writeBytes(crlf);
FileInputStream fileInputStream = new FileInputStream(file);
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
request.write(buffer, 0, bufferSize);
if (!data.getUploadPath().isEmpty()) {
String fieldName = "file";
String fileName = file.getName();
request.writeBytes(twoHyphens + boundary + crlf);
request.writeBytes("Content-Disposition: form-data; name=\"" +
fieldName + "\";filename=\"" +
fileName + "\"" + crlf);
request.writeBytes(crlf);
FileInputStream fileInputStream = new FileInputStream(file);
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
request.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
request.writeBytes(crlf);
}
request.writeBytes(crlf);
request.writeBytes(twoHyphens + boundary +
twoHyphens + crlf);
request.flush();
request.close();
}else {
request.close();
}else{
OutputStream os = con.getOutputStream();
os.write(data.getBody().getBytes("UTF-8"));
os.close();
}
}
} else{
con.setRequestProperty("Content-Type",content_type);
}
int status = con.getResponseCode();
responseData.setStatus(status);
if (status == HttpURLConnection.HTTP_OK) {
if (status == HttpURLConnection.HTTP_OK || status == HttpURLConnection.HTTP_ACCEPTED) {
InputStream responseStream = new
BufferedInputStream(con.getInputStream());
responseData.setResponseHeaders(con.getHeaderFields());
responseData.setSuccessful(true);
//Save files
if (responseData.getRequestData().getOutput().equals("-null")){
File f = new File(System.getProperty("java.io.tmpdir"),"insomnia");
try{
if (responseData.getRequestData().getOutput().equals("-null")) {
File f = new File(System.getProperty("java.io.tmpdir"), "insomnia");
try {
FileOutputStream outputStream = new FileOutputStream(f);
int read;
byte[] bytes = new byte[1024];
......@@ -162,18 +173,23 @@ public class RequestLoader {
while ((read = responseStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
}catch (IOException e) {
} catch (IOException e) {
e.printStackTrace();
}
responseData.setSize(f.length());
responseData.res = new String(Files.readAllBytes(Paths.get(f.getAbsolutePath())), Charset.forName("utf-8"));
f.delete();
}else {
Saver.saveResponse(responseData,responseStream);
} else {
Saver.saveResponse(responseData, responseStream);
}
if (data.isSaveRequest()) Saver.saveRequest(data);
con.disconnect();
}else if (status == HttpsURLConnection.HTTP_NO_CONTENT){
responseData.setSuccessful(true);
responseData.res = "No body returned for response";
con.disconnect();
} else {
responseData.setSuccessful(false);
......
......@@ -87,6 +87,7 @@ public class ResponseData implements Serializable {
String res = "";
public String getResponseString(){
return res;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment