static void PostData() { try { // Create a http request to the server endpoint that will pick up the // file and file description. HttpWebRequest requestToServerEndpoint = (HttpWebRequest)WebRequest.Create("http://192.168.4.226:80/era/applications?appid=909_99"); string boundaryString = String.Format("----------{0:N}", Guid.NewGuid()); string fileUrl = @"C:\Users\filename-1.0.par"; // Set the http request header \\ requestToServerEndpoint.Method = WebRequestMethods.Http.Post; requestToServerEndpoint.ContentType = "multipart/form-data; boundary=" + boundaryString; requestToServerEndpoint.KeepAlive = true; NetworkCredential nc = new NetworkCredential("admin", "admin"); requestToServerEndpoint.Credentials = nc; MemoryStream postDataStream = new MemoryStream(); StreamWriter postDataWriter = new StreamWriter(postDataStream); // Include value from the myFileDescription text area in the post data //postDataWriter.Write("\r\n--" + boundaryString + "\r\n"); //postDataWriter.Write("Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}", // "package.par", "package.par"); // Include the file in the post data postDataWriter.Write("\r\n--" + boundaryString + "\r\n"); postDataWriter.Write("Content-Disposition: form-data;" + "name=\"{0}\";" + "filename=\"{1}\"" + "\r\nContent-Type: {2}\r\n\r\n", "package.par", Path.GetFileName(fileUrl), Path.GetExtension(fileUrl)); postDataWriter.Flush(); //read FileStream fileStream = new FileStream(fileUrl, FileMode.Open, FileAccess.Read); byte[] buffer = new byte[1024]; int bytesRead = 0; while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) { postDataStream.Write(buffer, 0, bytesRead); } fileStream.Close(); ////////// postDataWriter.Write("\r\n--" + boundaryString + "--\r\n"); postDataWriter.Flush(); // Set the http request body content length requestToServerEndpoint.ContentLength = postDataStream.Length; // Dump the post data from the memory stream to the request stream using (Stream s = requestToServerEndpoint.GetRequestStream()) { postDataStream.WriteTo(s); } postDataStream.Close(); //////response bool b= requestToServerEndpoint.HaveResponse; WebResponse response = requestToServerEndpoint.GetResponse(); StreamReader responseReader = new StreamReader(response.GetResponseStream()); string replyFromServer = responseReader.ReadToEnd(); HttpWebResponse response2 = (HttpWebResponse)requestToServerEndpoint.GetResponse(); string returnString = response2.StatusCode.ToString(); System.Console.WriteLine("response=" + returnString); if (returnString.Equals("OK")) { System.Console.WriteLine("Request Successful"); } else { System.Console.WriteLine("Request Unsuccessful"); } } catch (WebException wex) { Stream c = wex.Response.GetResponseStream(); System.Console.WriteLine("Exception=" + wex.Message); // Exception message and stack trace Console.WriteLine(wex.Message); Console.WriteLine(wex.StackTrace); // Inspect the reason of communication failure switch (wex.Status) { case WebExceptionStatus.ConnectFailure: // handle failure to connect to server break; case WebExceptionStatus.ReceiveFailure: // handle failure to receive complete // HTTP response from server break; case WebExceptionStatus.Timeout: // handle timeout when waiting for // HTTP response from server break; case WebExceptionStatus.ConnectionClosed: // handle connection with server closed // prematurely break; // This is where we can examine HTTP status // codes other than 2xx and the server response // body case WebExceptionStatus.ProtocolError: // Examine the HTTP response returned HttpWebResponse response = (HttpWebResponse)wex.Response; StreamReader responseReader = new StreamReader(response.GetResponseStream()); string responseFromServer = responseReader.ReadToEnd(); switch (response.StatusCode) { case HttpStatusCode.BadRequest: // handle bad request (http status code 400) break; case HttpStatusCode.InternalServerError: // handle internal server error (http status code 500) break; default: // handle other http status code returned by // the server. break; } // end switch(res.StatusCode) response.Close(); break; case WebExceptionStatus.NameResolutionFailure: // handle DNS error break; default: // handle other errors not in this switch statement break; } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)