Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6999677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:34:23+00:00 2026-05-27T20:34:23+00:00

Reference to previous question: Getting an ajax response in java from a web method

  • 0

Reference to previous question: Getting an ajax response in java from a web method (java.io.FileNotFoundException)

I try to send a POST request containing json in the request body but the body is encoded differently when doing it from a web browser (chrome) and my java code.
Here is my code:

String params = "{\"prefixText\":\"aCity\",\"count\":10,\"contextKey\":\"he\"}";
conn = (HttpURLConnection) new URL("http://bus.gov.il/WebForms/wfrmMain.aspx/GetCompletionList").openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setFixedLengthStreamingMode(params.length());
conn.addRequestProperty("Content-Type", "application/json; charset=UTF-8");
conn.addRequestProperty("Referer", "http://bus.gov.il/WebForms/wfrmMain.aspx?width=1024&company=1&language=he&state=");
Writer out = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream()));
out.write(params);
out.close();
String answer = readStream(conn.getInputStream(), "utf-8");

Here are the 2 requests sent as seen in Wireshark: link (first is the good one, second is the wrongly encoded java request).

I tried changing the charset in Content-Type but it did nothing.

Any help will be highly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:34:23+00:00Added an answer on May 27, 2026 at 8:34 pm

    Here, you’re using the default character encoding for your system:

    Writer out = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream()));
    

    Instead, specify the encoding explicitly in the constructor call for OutputStreamWriter:

    Writer out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
    

    (You don’t need the BufferedWriter here – it’s not doing you any good.)

    EDIT: Okay, you’re now getting a problem because you’re setting the content length incorrectly. You should convert from text to binary once, like this:

    byte[] data = params.getBytes("UTF-8");
    conn.setFixedLengthStreamingMode(data.length);
    ...
    OutputStream output = conn.getOutputStream();
    // Note: you should use a finally block here
    output.write(data);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following on from my previous question, I am getting another compilation error on my
In reference to my previous question: One table, two column MYSQL query challenge The
With a reference to my previous question , I would like to know how
As per my previous question: Rhino Mocks - Testing Repository layer returns "object reference
I cut&pasted the below code from a previous question into a file called avishay.cpp
This is a continuation question from a previous question I have asked I now
Following up my previous question , I'm slowly getting the hang of FParsec (though
With reference to my previous question, How can i implement AND and OR operations
In a previous question, I asked how to send text to Notepad . It
With reference to to an previous question of mine: link I have an additional

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.