Web2py uploading big files ssl fails - java

i hope get some help with this post.
I built a java console app that sends file to my web2py server.
This procedure just works to small files, when i try to send big file with 300Mb, i got a ssl fail.
In java side I have this code below to define my request:
URL url = new URL(requestURL);
httpConn = (HttpURLConnection) url.openConnection();
httpConn.setUseCaches(false);
httpConn.setDoOutput(true); // indicates POST method
httpConn.setDoInput(true);
httpConn.setConnectTimeout(TIME_OUT);
httpConn.setReadTimeout(TIME_OUT);
httpConn.setRequestProperty("Content-Type",
"multipart/form-data; boundary=" + boundary);
httpConn.setRequestProperty("User-Agent", "CodeJava Agent");
httpConn.setChunkedStreamingMode(1024);
outputStream = httpConn.getOutputStream();
writer = new PrintWriter(new OutputStreamWriter(outputStream, charset),
true);
The exception returned in my java console application:
java.io.IOException: Error writing request body to server
And this in my server side:
HTTP/1.1 400 Bad RequestContent-Length: 11Content-Type: text/plainBad Request
If I comment this line: "httpConn.setChunkedStreamingMode(1024);", i don't get this output anymore... But I got a Java heap space out of memory.
I have no idea how to solve that. I have searched in google but i got nothing.

The peer has closed the connection but you are still sending. Probably you have exceeded some upload size limit.

Related

HttpUrlConnection method is always GET on android

URL url = new URL("http://myserver.com/myendpoint");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
//connection.setRequestMethod("POST") <- this didn't help either
connection.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write("string=test");
out.close();
connection.close()
The code above WORKS on desktop JVM, sends a post request, parsed on server-side successfully with response 200, however on android, the request method stays GET (yes I checked it IS false) and results in a 404 exception. Official docs say that setting doOutput to true triggers setting the request method to POST but that doesn't seem the case.
404 is not an exception. It is a HTTP status code returned by the server you make the request to and it means that the url you make the request to is not found. That has nothing to do with POST being set or not.
Things to check:
If the url you are making a request to is right.
If the server has a POST controller/handler mapped to the url you are making the request to.
Ask the guy who develops the server if he is handling the cases right ans if he's sending the correct response codes for the relevant scenarios.
Extra info: if the url is registered on the service but a POST request is not allowed you would get a 415 response code.
When posting data to a server, I'm setting some additional request header:
String query = "string=test";
URL url = new URL("http://myserver.com/myendpoint");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Accept-Charset", "UTF-8");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
connection.setFixedLengthStreamingMode(query.getBytes("UTF-8").length);
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write(query);
But as suggested, the 404 exception usually means, that the endpoint, you're trying to access, isn't available.
Try it:
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");

Calling an web service from java program

I am trying to call an web service by sending an xml file as input and i should be receiving an xml as a reply but whenever i send the xml i get proxy authentication error so i thought i was sending the wrong xml but the same xml works fine when i use SOAP UI so i guess there is some problem with my code.
Here is the code below
URL url = null;
String strUrl="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope\" xmlns:soap=\"SoapAuthenticator\" xmlns:ship=\"http://ws.consignorsupport.no/ShipAdvisor\"><soapenv:Header> <soap:ServiceAuthenticationHeader><soap:Username>TDC43671</soap:Username> <soap:Password>hTiNMft/KaMfDDD</soap:Password><soap:IsEncrypted>false</soap:IsEncrypted></soap:ServiceAuthenticationHeader></soapenv:Header><soapenv:Body><ship:SearchForDropPoints><ship:productConceptID>92</ship:productConceptID><ship:installationID>00000000018</ship:installationID><ship:country>DK</ship:country><ship:address></ship:address><ship:postCode>6000</ship:postCode><ship:city></ship:city><ship:limit>5</ship:limit></ship:SearchForDropPoints></soapenv:Body></soapenv:Envelope>";
String ss="http://www.consignorsupport.no/ShipAdvisor/Main.asmx";
url = new URL(ss);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestProperty("Content-type", "text/xml");
conn.setRequestProperty("Accept", "text/xml, application/xml");
conn.setRequestMethod("POST");
conn.connect();
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(strUrl);
wr.flush();
wr.close();
int iHttpResponseCode = conn.getResponseCode();
String strErrorMessage = conn.getResponseMessage();
System.out.println("Getting Response status");
System.out.println(iHttpResponseCode);
System.out.println(strErrorMessage);
Can anybode help me as to where i am going wrong.
If you get a proxyauthentication error, chances are that that really is the issue. Maybe soap ui is set up with the correct proxy info? Or gets it from the system settings? Your java code won't automatically pick up on these settings. Check if you have a system proxy configured, or one in soapUI.

Java: HTTP POST 405 Not Allowed

I stuck in amazing situation while consuming REST operation.
When i run simple java program in IDE then i am able to create HTTP request: 201/Created. SUCCESS. Server receives request
When i put same program in Runtime environment(say Mule), i get 405/Method not Allowed. FAILED. Server does not receive anything
When i run it through Rest Client(some mozilla or chrome plugin), It accepts request 201/created. SUCCESS. Server receives request
On that particular URL, i am getting following Allowed:
Allow: GET, POST
Is this issue with Runtime?
or any settings to be done in Runtime environment?
or issue in Code?
Following is Code:
`URL urlRequest = new URL(url);
HttpURLConnection conn = (HttpURLConnection) urlRequest.openConnection();
conn.setRequestProperty("Host", "10.91.17.170:81");
conn.setRequestProperty("User-Agent", "Mozilla/4.0");
conn.setRequestProperty("Accept", "application/atom+xml;q=0.9,*/*;q=0.8");
conn.setRequestProperty("Accept-Language", "en-us,en;q=0.5");
conn.setRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
conn.setRequestProperty("Keep-Alive", "115");
conn.setRequestProperty("Connection", "keep-alive");
conn.setRequestProperty("Access-Control-Allow-Origin", "*");
conn.setRequestProperty("Content-Type", "application/atom+xml");
conn.setDoOutput(true);
conn.setRequestMethod("POST");
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.writeBytes(requestXML);
wr.flush();
wr.close();
System.out.println("Created Request");
System.out.println("Response Code: "+conn.getResponseCode());
System.out.println("Response Status: "+conn.getResponseMessage());
System.out.println("Response Status: "+conn.getContentType());
System.out.println("Request Method: "+conn.getRequestMethod());
`
Unable to get answer on google. Did hit and trials but not successful.
405 errors often arise with the POST method. You may be trying to introduce some kind of input form on the Web site, but not all ISPs allow the POST method necessary to process the form.
All 405 errors can be traced to configuration of the Web server and security governing access to the content of the Web site, so should easily be explained by your ISP.
for details you can see this:
jQuery .ajax() POST Request throws 405 (Method Not Allowed) on RESTful WCF

Google Docs API returns a 503 Service Unavailable when uploading a file. Has something changed?

I have applications that make use of the Google Docs API. Up until recently, uploads using the HTTP endpoints has been working fine. Recently, uploading has suddenly started erroring. The first call to create a session (which returns the resumable URL) works fine, and returns a resumable URL. Attempting to then send the file contents to the resumable URL throws a 503.
The relevant part of the code throwing the error is this:
URL url = new URL(resumableFileUploadUrl);
conn = (HttpURLConnection) url.openConnection();
conn.addRequestProperty("client_id", OAuth2Client.CLIENT_ID);
conn.addRequestProperty("client_secret", OAuth2Client.CLIENT_SECRET);
conn.setRequestProperty("Authorization", "OAuth " + GetAuthToken());
conn.setRequestProperty("X-Upload-Content-Length", String.valueOf(fileContents.length())); //back to 0
conn.setRequestProperty("X-Upload-Content-Type", "text/xml");
conn.setRequestProperty("Content-Type", "text/xml");
conn.setRequestProperty("Content-Length", String.valueOf(fileContents.length()));
conn.setRequestProperty("Slug", fileName);
if(isUpdate)
{
conn.setRequestProperty("If-Match", "*");
conn.setRequestMethod("PUT");
}
else
{
conn.setRequestMethod("POST");
}
conn.setRequestProperty("GData-Version", "3.0");
conn.setRequestProperty("User-Agent", "GPSLogger for Android");
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(
conn.getOutputStream());
wr.writeBytes(fileContents);
wr.flush();
wr.close();
int code = conn.getResponseCode();
newLocation = conn.getHeaderField("location");
The above code is used both for creating the session to get the resumable URL as well as posting file contents to the resumable URL.
Which is part of this Android activity. I am including a link to the original activity as it would probably be quite easy to reproduce the problem by simply cloning the repository. The code has remained untouched for a year.
Has something changed recently that would cause this?
I would like to avoid having to use Google Drive's APIs for now as I have not changed any code and the same code is being used in a few of my other applications in the field.
One of the most annoying “features” is the undocumented rate limit when requesting images, each request of which must be authenticated. The rate limit appears to be around 10/s.
You may be using Google API v2 which is deprecated as of now. Read the below post:
https://developers.google.com/google-apps/documents-list/terms

Getting an ajax response in java from a web method (java.io.FileNotFoundException)

Don't know how to explain it better but i'm trying to get a response from an URL containing a function (right?).
I'm working on this for a lot of hours and progressing a little every time but can't get this finally working.
This is the request and response headers from chrome dev tools:
Headers
My code is:
String params = "{\"prefixText\":\"" + city
+ "\",\"count\":10,\"contextKey\":\"he\"}";
conn = (HttpURLConnection) new URL(
"http://bus.gov.il/WebForms/wfrmMain.aspx/GetCompletionList")
.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setChunkedStreamingMode(0);
// conn.setFixedLengthStreamingMode(params.length());
conn.addRequestProperty("Accept", "*/*");
conn.addRequestProperty("Content-Type", "application/json; charset=UTF-8");
conn.addRequestProperty("Content-Length", String.valueOf(params.length()));
conn.addRequestProperty("Host", "bus.gov.il");
conn.addRequestProperty("Origin", "http://bus.gov.il");
conn.addRequestProperty("X-Requested-With", "XMLHttpRequest");
conn.addRequestProperty("Referer",
"http://bus.gov.il/WebForms/wfrmMain.aspx?width=1024&company=1&language=he&state=");
OutputStream os = new BufferedOutputStream(conn.getOutputStream());
os.write(params.getBytes());
String answer = readStream(conn.getInputStream());
I get the exception (I see in the stack trace) when calling "getinputstream" on this line:
String answer = readStream(conn.getInputStream());
before entering the readStream function!
I don't know how to solve it...
Tried searching about xmlhttprequest but understood that it's only in JS.
Also: I know I have a lot of unnecessary request properties but I can't figure out which are unnecessary until the code will work.
Thanks in advance :)
Sadly, it used to be (and probably still is) that the HttpURLConnection throws a FileNotFoundException when you get a 404 error. When you are doing the getInputStream() that's when it's first connecting, so any error from the server will show up there.
Get Wireshark or something if you want to see what's really going on in HTTP land as you make the request.

Categories