dcsimg
Again, How to send a HTTP request to servlet in java application
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   flora_chen
Posted On:   Thursday, April 1, 2004 02:51 PM

based on Brian's replies to my previous post, I wrote a java class, which send a HTTP request to servlet. It can be compiled but always threw an exceptions when I ran it. Can anyone help. Here is the code: package coreservlets; import java.io.*; import java.net.*; public class PostHTTP { public static void main(String args[]) throws IOException, UnknownHostException { try { URL myURL = new URL("http://pc076/servlet/coreservlets.OffHold"); URLConnection c = myURL.openConnection(); c.setUseCaches(false); c.setDoOutput(true); ByteArrayOutputStream byteStream = new ByteArrayOutputStream(512); P   More>>

based on Brian's replies to my previous post, I wrote a java class, which send a HTTP request to servlet. It can be compiled but always threw an exceptions when I ran it. Can anyone help. Here is the code:

			

package coreservlets;

import java.io.*;
import java.net.*;

public class PostHTTP
{
public static void main(String args[])
throws IOException, UnknownHostException {
try
{

URL myURL = new URL("http://pc076/servlet/coreservlets.OffHold");
URLConnection c = myURL.openConnection();
c.setUseCaches(false);
c.setDoOutput(true);

ByteArrayOutputStream byteStream = new ByteArrayOutputStream(512);

PrintWriter out = new PrintWriter(byteStream, true);
String postData = "REASON_CODE=3B&RSPCODE=JSmith&CASENUM=NA795401&REPLY=123&SOURCE=XYZ&REPLYLINK=http://pc076/servlet/coreservlets.ShowParameters";
out.print(postData);
out.flush();

String lengthString = String.valueOf(byteStream.size());
c.setRequestProperty("Content-Length", lengthString);
c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

byteStream.writeTo(c.getOutputStream());

BufferedReader in = new BufferedReader(new InputStreamReader
(c.getInputStream()));

String line;
//String linefeed = "
";
//resultsArea.setText("");
while((line = in.readLine()) != null) {
System.out.println(line);
//resultsArea.append(linefeed);
}
}

catch(IOException ioe) {
// Print debug info in Java Console
System.out.println("IOException: " + ioe);
}
}
}

   <<Less

Re: Again, How to send a HTTP request to servlet in java application

Posted By:   Brian_Glodde  
Posted On:   Friday, April 2, 2004 11:28 AM

You may be running into trouble trying to write output to a server that doesn't allow it. For example, an HTTP response code of 501 would indicate you cannot post in this manner (you can't use this on Google, for example)...in short, it won't allow you to use the line c.doSetOutput(true);. Here is simpler example, this time using a GET to pull back search results from Google.com.


import java.io.*;
import java.net.*;

public class PostHTTP {
public static void main(String args[]) throws IOException,
UnknownHostException {
try {
URL url = new URL(
"http://www.google.com/search?hl=en&ie=UTF-8&oe=UTF-8&q=http+post+url+java");
URLConnection conn = url.openConnection();
conn.setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible;MSIE 5.5; Windows NT 5.0; H010818)");

// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.
getInputStream()));
String line;
while ( (line = rd.readLine()) != null) {
System.out.println(line);
}
rd.close();
}
catch (Exception e) {
System.out.println(e.toString());
}
}
}
About | Sitemap | Contact