Some time ago, I received a request to dock a tool class written by C # and upload data to our system background.
Demand is not difficult, very common, so for convenience. So I wrote (java framework SSH):
C# Code to simulate requests
public static void Main(string[] args) { String postData = fileToString("D:\\test\\json.txt"); Console.WriteLine(postData); string url = "ip:port/Project name/tensionGroution.do?method=uploadData&data=" + postData; Console.WriteLine("******************************************"); if (postData != null) PostUrl(url, postData); Console.ReadKey(); } public static string PostUrl(string url, string postData) { string result = ""; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.KeepAlive = false; req.ProtocolVersion = HttpVersion.Version10; req.Method = "POST"; req.Timeout = 80000;//Set the request timeout time in milliseconds req.ContentType = "application/json"; byte[] data = Encoding.UTF8.GetBytes(postData); req.ContentLength = data.Length; using (Stream reqStream = req.GetRequestStream()) { reqStream.Write(data, 0, data.Length); reqStream.Close(); } HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); Stream stream = resp.GetResponseStream(); //Getting response content using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { result = reader.ReadToEnd(); } Console.WriteLine(postData); return result; } public static string fileToString(String filePath) { if (!File.Exists(filePath)) { Console.WriteLine("file does not exist"); return null; } string strData = ""; try { string line; // Create an instance of StreamReader to read files, and the using statement can also close StreamReader using (System.IO.StreamReader sr = new System.IO.StreamReader(filePath, System.Text.Encoding.GetEncoding("gb2312"))) { // Read and display rows from the file until the end of the file while ((line = sr.ReadLine()) != null) { //Console.WriteLine(line); strData += line; } } } catch (Exception e) { // Display an error message to the user Console.WriteLine("The file could not be read:"); Console.WriteLine(e.Message); } return strData; } }
Provides the upload path, and carries parameters (json format) to simulate http post requests through the http WebRequest object of C #. The request is sent normally.
The java backend also receives it in a very common way: String data = request.getParameter("data");
ok completed the deployment, the test is correct. But after a while, the problem arises.
C
The reason for the investigation was originally caused by the parameters of the request interface.
When the request protocol with too long parameters refuses to send, an error is reported.
Solution:
In C # the code that simulates the request finds such a class that it actually operates on the parameters.
Stream is an object that actually writes parameters to requests in the form of streams, and previous operations are useless.
Once you understand Stream's principles, you change the request interface, not to carry parameters, Stream objects to process.
String url = ip: port / project name / tensionGroution.do?method=uploadData; (modified interface).
Then the java background acceptance of this parameter also needs to be processed, apparently read stream.
String tradeInfos = ""; BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(request.getInputStream(), "utf-8")); // Read parameter stream String nextLine = bufferedReader.readLine(); while (nextLine != null) { System.out.println(request.getCharacterEncoding()); tradeInfos += nextLine; nextLine = bufferedReader.readLine(); } System.out.println(tradeInfos);
In this way, we can get the data uploaded by C# tool smoothly.
Of course, there are other ways of request and acceptance, to find the most appropriate is the best!!