Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I cant tell you what website or anything like that, because my work is private for the time being. But i will try to give you as much info as needed to solve my problem.

I am working on a problem that can remote-operate a website-thingie through a HTTPConnection in java.

This is my void:

Java
public static String excutePost(String targetURL, String urlParameters)
	/*     */   {
		System.out.println("Executing post");
	/*  64 */     HttpURLConnection connection = null;
	/*     */     try
	/*     */     {
	/*  67 */       URL url = new URL(targetURL);
					URLConnection conn = url.openConnection();
					connection = (HttpURLConnection)conn;
					//connection.addRequestProperty("COOKIE", "yes");
	/*  69 */       connection.setRequestMethod("POST");
	//connection.addRequestProperty("Accept-Charset", "UTF-8");
	/*  70 */       connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
	/*     */ 
	/*  72 */       connection.setRequestProperty("Content-Length", Integer.toString(urlParameters.getBytes().length));
	/*  73 */       //connection.setRequestProperty("Content-Language", "en-US");
	/*     */ 
	/*  75 */       connection.setUseCaches(false);
	/*  76 */       connection.setDoInput(true);
	/*  77 */       connection.setDoOutput(true);
	if(loginComplete)
	{
		for (String cookie : cookies) {
			String[] in = cookie.split("; ");
			for(int i = 0; i < 1; i++)
			{
				connection.setRequestProperty("Cookie", in[i]);
				//System.out.println("Proper out: " + in[i]);
			}
		}
		for(int i = 0; i < cookies.size(); i++)
		{
			//System.out.println("Out: " + cookies.get(i));
		}
	}

	// ...
	
	// ...

	/*  80 */       connection.connect();
	/*     */ 
	/*  97 */       DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
	/*  98 */       wr.writeBytes(urlParameters);
	/*  99 */       wr.flush();
	/* 100 */       wr.close();
	/*     */ 
	/* 103 */       InputStream is = connection.getInputStream();
	/* 104 */       BufferedReader rd = new BufferedReader(new InputStreamReader(is));
	/*     */ 
	/* 106 */       String response = "";
	/*     */       String line;
	/* 107 */       while ((line = rd.readLine()) != null)
	/*     */       {
	/* 108 */         response = response + line;
	System.out.println(line);
	/* 109 */         response = response + '\r';
	Thread.sleep(5);
	/*     */       }
	/* 111 */       rd.close();
	/*     */ 
	/* 115 */       String str1 = response.toString();
	if(!loginComplete)
	{
		/*     */ 		// Gather all cookies on the first request.
		cookies = connection.getHeaderFields().get("Set-Cookie");
		for(int i = 0; i < cookies.size(); i++)
		{
			//System.out.println(cookies.get(i));
		}
	}
	System.out.println("Done!");
	/*     */       return str1;
	/*     */     }
	/*     */     catch (Exception e)
	/*     */     {
	/* 119 */       e.printStackTrace();
	/*     */       return "";
	/*     */     }
	/*     */     finally
	/*     */     {
	/* 124 */       if (connection != null)
	/* 125 */         connection.disconnect();
	/*     */     }
	/* 127 */     //throw localObject;
	/*     */   }


Pardon the line numbers, i had to decommpile a older program of mine that used this code, as i kinda lost the source...

But anyways, when i run the program, i print each line to the console for debug reasons. I found out that it stops at the same space each time, so i am guessing a limit in the BufferedReader code. Any ideas?
Posted

1 solution

There is no limit - I assume your code stops at the same point. Probably where the connection is made up.

To keep your project "private" you should use a local webserver:

XAMPP[^] is free, easy to use and works like a charm.

I can recommend Log4J [^]for proper logging, console print outs are the dirty way of doing it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900