Re: Http url connection : server returned http response code 400

2013-11-16 Thread vicky b
THanks you, can you please let me know what optimization I have to make.

  con.setRequestProperty("Referer", request.getHeader("Referer"));

was the issue , since it was being set to null


On Mon, Nov 11, 2013 at 7:38 AM, Christopher Schultz <
ch...@christopherschultz.net> wrote:

> -BEGIN PGP SIGNED MESSAGE-
> Hash: SHA256
>
> Vicky,
>
> On 11/11/13, 10:02 AM, vicky b wrote:
> > I am  getting server returned http response code 400  when  i run
> > below code from my tomcat however it works fine when i run it in
> > WAS whch has proxy server   settings.
> >
> > URL url = new URL(reqUrl); HttpURLConnection con =
> > (HttpURLConnection)url.openConnection(); con.setDoOutput(true);
> > con.setRequestMethod(request.getMethod());
> > if(request.getContentType() != null) {
> > con.setRequestProperty("Content-Type", request.getContentType());
> > } con.setRequestProperty("Referer", request.getHeader("Referer"));
> > int clength = request.getContentLength(); if(clength > 0) {
> > con.setDoInput(true); InputStream istream =
> > request.getInputStream(); OutputStream os = con.getOutputStream();
> > final int length = 5000; byte[] bytes = new byte[length]; int
> > bytesRead = 0; while ((bytesRead = istream.read(bytes, 0, length))
> > > 0) { os.write(bytes, 0, bytesRead); } } else {
> > con.setRequestMethod("GET"); } out.clear(); out =
> > pageContext.pushBody(); OutputStream ostream =
> > response.getOutputStream(); System.out.println(" finished
> > getOUTputsteram"); response.setContentType(con.getContentType());
> > InputStream in = con.getInputStream(); final int length = 5000;
> > byte[] bytes = new byte[length]; int bytesRead = 0; while
> > ((bytesRead = in.read(bytes, 0, length)) > 0) {
> > ostream.write(bytes, 0, bytesRead); }
>
> So you have a quick-and-dirty proxy servlet, right?
>
> There are a number of optimizations, etc that you should probably
> make, but none of the above code uses any Tomcat code (other than
> fetching information from the incoming request, which presumably works
> correctly).
>
> It looks like you are getting a 400 from the server your code is
> contacting. Have you looked at the request that is actually being sent?
>
> Under what conditions do you get a 400 response? I see you are
> unconditionally setting con.setDoOutput(true) even if you don't intend
> to send any data. You conditionally call setDoInput which doens't make
> a great deal of sense. I think you have these two calls reversed in
> your head.
>
> - -chris
> -BEGIN PGP SIGNATURE-
> Version: GnuPG v1.4.15 (Darwin)
> Comment: GPGTools - http://gpgtools.org
> Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/
>
> iQIcBAEBCAAGBQJSgPnjAAoJEBzwKT+lPKRYg7QP/2DIfwXWl/d5XTXxLwn637H7
> voM3qF26zyoObW5F/Z2TmTDnidmTb/D4PSn/ZE+1cKJcbgBWoY36fJ+MFaAXhptf
> 5MQet22E7xCUWs0n9Y9QbcPA7J5ZaGDap4O3ukW5C8O4/+vPkkNnjlIycNRa/P+/
> UvfvZVxVeZR2xioar4L81gr0CgBzCALUjCPQ5pLqh14NOMzl9nfNbxmkCUvQYpGa
> YpWmVhF7QKuv08fJxNLzEzjuZS+gQZn1SdqaMWoO3ebUrWMIpNiiv/xZC/oJInIT
> qfxvDrRpRpzzbhIWUwdfj4PzXHDJz0OlUVN5UzDY0WocDMddN1QeaRuQaZQnLpsK
> Cy27E8wWJrATd4vdki7FteQCIyZBPB6A/sBy0nKSArvn13uGowcdgPpTsc+sW5hP
> GxRiohnpH9vWj2IMDSuBtnnipDp/+f7JsvjQqOQ2Pmw2Zs6BwCAG8v7ufDFil6Lj
> NDhIPJ7FFeCmso+DeWItmBQq7iQV7mQaZ9DKhS9y/1hd0ZF/kqTv5KgKzpu12BTE
> 2GauCWxIt9qiCuVgNEpp4cEsV4sptxV+XI4k5DtFVB0EMcX9gGVNYjxJSQP/4efG
> tjgA6jG2Ea/YM2KU2cJu0F2mFL3qKr3Vh47K5NE+mHtFMhrygz8nH5zX+s9nvF6F
> 5vrRBravwnLLOk/ZGOU2
> =Zxr5
> -END PGP SIGNATURE-
>
> -
> To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
> For additional commands, e-mail: users-h...@tomcat.apache.org
>
>


-- 



*Thanks & Regards Vickyb*


Re: Http url connection : server returned http response code 400

2013-11-11 Thread Christopher Schultz
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA256

Vicky,

On 11/11/13, 10:02 AM, vicky b wrote:
> I am  getting server returned http response code 400  when  i run
> below code from my tomcat however it works fine when i run it in
> WAS whch has proxy server   settings.
> 
> URL url = new URL(reqUrl); HttpURLConnection con =
> (HttpURLConnection)url.openConnection(); con.setDoOutput(true); 
> con.setRequestMethod(request.getMethod()); 
> if(request.getContentType() != null) { 
> con.setRequestProperty("Content-Type", request.getContentType()); 
> } con.setRequestProperty("Referer", request.getHeader("Referer")); 
> int clength = request.getContentLength(); if(clength > 0) { 
> con.setDoInput(true); InputStream istream =
> request.getInputStream(); OutputStream os = con.getOutputStream(); 
> final int length = 5000; byte[] bytes = new byte[length]; int
> bytesRead = 0; while ((bytesRead = istream.read(bytes, 0, length))
> > 0) { os.write(bytes, 0, bytesRead); } } else { 
> con.setRequestMethod("GET"); } out.clear(); out =
> pageContext.pushBody(); OutputStream ostream =
> response.getOutputStream(); System.out.println(" finished
> getOUTputsteram"); response.setContentType(con.getContentType()); 
> InputStream in = con.getInputStream(); final int length = 5000; 
> byte[] bytes = new byte[length]; int bytesRead = 0; while
> ((bytesRead = in.read(bytes, 0, length)) > 0) { 
> ostream.write(bytes, 0, bytesRead); }

So you have a quick-and-dirty proxy servlet, right?

There are a number of optimizations, etc that you should probably
make, but none of the above code uses any Tomcat code (other than
fetching information from the incoming request, which presumably works
correctly).

It looks like you are getting a 400 from the server your code is
contacting. Have you looked at the request that is actually being sent?

Under what conditions do you get a 400 response? I see you are
unconditionally setting con.setDoOutput(true) even if you don't intend
to send any data. You conditionally call setDoInput which doens't make
a great deal of sense. I think you have these two calls reversed in
your head.

- -chris
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.15 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQIcBAEBCAAGBQJSgPnjAAoJEBzwKT+lPKRYg7QP/2DIfwXWl/d5XTXxLwn637H7
voM3qF26zyoObW5F/Z2TmTDnidmTb/D4PSn/ZE+1cKJcbgBWoY36fJ+MFaAXhptf
5MQet22E7xCUWs0n9Y9QbcPA7J5ZaGDap4O3ukW5C8O4/+vPkkNnjlIycNRa/P+/
UvfvZVxVeZR2xioar4L81gr0CgBzCALUjCPQ5pLqh14NOMzl9nfNbxmkCUvQYpGa
YpWmVhF7QKuv08fJxNLzEzjuZS+gQZn1SdqaMWoO3ebUrWMIpNiiv/xZC/oJInIT
qfxvDrRpRpzzbhIWUwdfj4PzXHDJz0OlUVN5UzDY0WocDMddN1QeaRuQaZQnLpsK
Cy27E8wWJrATd4vdki7FteQCIyZBPB6A/sBy0nKSArvn13uGowcdgPpTsc+sW5hP
GxRiohnpH9vWj2IMDSuBtnnipDp/+f7JsvjQqOQ2Pmw2Zs6BwCAG8v7ufDFil6Lj
NDhIPJ7FFeCmso+DeWItmBQq7iQV7mQaZ9DKhS9y/1hd0ZF/kqTv5KgKzpu12BTE
2GauCWxIt9qiCuVgNEpp4cEsV4sptxV+XI4k5DtFVB0EMcX9gGVNYjxJSQP/4efG
tjgA6jG2Ea/YM2KU2cJu0F2mFL3qKr3Vh47K5NE+mHtFMhrygz8nH5zX+s9nvF6F
5vrRBravwnLLOk/ZGOU2
=Zxr5
-END PGP SIGNATURE-

-
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org



Http url connection : server returned http response code 400

2013-11-11 Thread vicky b
HI All,

  I am  getting server returned http response code 400  when  i run below
code from my tomcat however it works fine when i run it in WAS whch has
proxy server   settings.

URL url = new URL(reqUrl);
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setDoOutput(true);
con.setRequestMethod(request.getMethod());
if(request.getContentType() != null) {
  con.setRequestProperty("Content-Type", request.getContentType());
}
  con.setRequestProperty("Referer", request.getHeader("Referer"));
int clength = request.getContentLength();
if(clength > 0) {
con.setDoInput(true);
InputStream istream = request.getInputStream();
OutputStream os = con.getOutputStream();
final int length = 5000;
  byte[] bytes = new byte[length];
  int bytesRead = 0;
  while ((bytesRead = istream.read(bytes, 0, length)) > 0) {
os.write(bytes, 0, bytesRead);
  }
}
  else {
con.setRequestMethod("GET");
  }
out.clear();
  out = pageContext.pushBody();
OutputStream ostream = response.getOutputStream();
System.out.println(" finished getOUTputsteram");
response.setContentType(con.getContentType());
 InputStream in = con.getInputStream();
 final int length = 5000;
  byte[] bytes = new byte[length];
  int bytesRead = 0;
  while ((bytesRead = in.read(bytes, 0, length)) > 0) {
ostream.write(bytes, 0, bytesRead);
  }
-- 



*Thanks & Regards Vickyb*