System.Net.WebException: The remote server returned an error: NotFound. ---> silverlight file post to WCF Rest Service - silverlight

i am trying to post an image to wcf rest service from silverlight 4 but i am getting an exception when i am taking resonse from the request, the exception is
System.Net.WebException: The remote server returned an error: NotFound. ---> System.Net.WebException: The remote server returned an error: NotFound.
at System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
at System.Net.Browser.BrowserHttpWebRequest.<>c_DisplayClassa.b_9(Object sendState)
at System.Net.Browser.AsyncHelper.<>c_DisplayClass4.b_1(Object sendState)
--- End of inner exception stack trace ---
at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
at System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at Admin.Popups.AddCategory.OnImageGetResponseCompleted(IAsyncResult ar)}
all of my other functions on the service is working fine even this one when i try to access it from android, i already had a look and tried the silverlight cross domain policy but i dont think its applicable, my code is below
private void UpLoadImage()
{
string URL = Utilities.GetServiceUrl() + "AddCategoryImage?CategoryId=2&ContentType=image&apikey=" + Utilities.GetApiKey();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.Method = "POST";
request.ContentType = "text/plain";
request.ContentLength = SelectedFile.Length;
request.BeginGetRequestStream(OnImageGetRequestStreamCompleted, request);
}
private void OnImageGetRequestStreamCompleted(IAsyncResult ar)
{
try
{
if (SelectedFile != null)
{
HttpWebRequest request = (HttpWebRequest)ar.AsyncState;
using (Stream postStream = request.EndGetRequestStream(ar))
{
using (Stream source = SelectedFile.OpenRead())
{
byte[] buffer = new byte[2048];
int bytesRead;
while ((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0)
{
postStream.Write(buffer, 0, bytesRead);
}
}
}
request.BeginGetResponse(new AsyncCallback(OnImageGetResponseCompleted), request);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void OnImageGetResponseCompleted(IAsyncResult ar)
{
try
{
HttpWebRequest request = (HttpWebRequest)ar.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(ar);
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
string result = sr.ReadToEnd();
}
}
catch (Exception ex)
{
this.Dispatcher.BeginInvoke(delegate()
{
MessageBox.Show(ex.Message);
});
}
}
the wcf function is
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "AddCategoryImage?CategoryId={CategoryId}&ContentType={ContentType}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
bool AddCategoryImage(int CategoryId, string ContentType, Stream Image);
and function implementation is
public static bool AddCategoryImage(int CategoryId, string ContentType,Stream Image)
{
SqlConnection sqlCon = new SqlConnection(Params.GetConnectionString());
SqlCommand sqlCmd = new SqlCommand("UPDATE Categories SET Cat_Img = #Cat_Img,Cat_Img_Type = #Cat_Img_Type WHERE Cat_ID = #Cat_ID", sqlCon);
sqlCmd.Parameters.Add("#Cat_Img", SqlDbType.Image).Value = Utility.ConvertStreamToByteArray(Image);
sqlCmd.Parameters.Add("#Cat_Img_Type", SqlDbType.NVarChar, 32).Value = ContentType;
sqlCmd.Parameters.Add("#Cat_ID", SqlDbType.Int).Value = CategoryId;
try
{
sqlCon.Open();
return sqlCmd.ExecuteNonQuery() > 0 ? true : false;
}
catch (Exception ex)
{
Error.CreateError(ex.Message, "Category.cs", "AddCategoryImg(int,string,Stream)");
}
finally
{
sqlCon.Close();
}
return false;
}

i have solved the issue WCF defualt maxReceivedMessageSize is 65K and i was trying to upload file more than 65k, clever me :) so i set the maxReceivedMessageSize="2000000" to 2MB now it works for files up to 2MB
<binding name ="webBinding" sendTimeout="00:10:00" receiveTimeout="00:10:00" openTimeout="00:10:00" closeTimeout="00:10:00" maxReceivedMessageSize="2000000">
<readerQuotas maxDepth="32" maxStringContentLength="100000000" maxArrayLength="100000000" maxBytesPerRead="100000000" maxNameTableCharCount="100000000" />
</binding>

Related

android app development-passing parameter to database

I am trying to connect my app to database on localhost server.I can connect to it ut the problem is how to pass the parameter from app to php script.for eg i want all names having age less than 10 so i will pass the parameter to php.below is my code for connecting to database.please provide good reference
/* */
enter code here
public class TestExternalDatabaseActivity extends Activity {
/** Called when the activity is first created. */
TextView resultView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
StrictMode.enableDefaults(); //STRICT MODE ENABLED
resultView = (TextView) findViewById(R.id.result);
getData();
}
public void getData(){
String result = "";
InputStream isr = null;
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost/tahseen0amin/php/getAllCustomers.php"); //YOUR PHP SCRIPT ADDRESS
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
isr = entity.getContent();
}
catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
resultView.setText("Couldnt connect to database");
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
isr.close();
result=sb.toString();
}
catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//parse json data
try {
String s = "";
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length();i++){
JSONObject json = jArray.getJSONObject(i);
s = s +
"Name : "+json.getString("FirstName")+" "+json.getString("LastName")+"\n"+
"Age : "+json.getInt("Age")+"\n"+
"Mobile Using : "+json.getString("Mobile")+"\n\n";
}
resultView.setText(s);
} catch (Exception e) {
// TODO: handle exception
Log.e("log_tag", "Error Parsing Data "+e.toString());
}
}
}

HttpWebRequest "POST" returning server error (Exception)

I'm trying to call the google URL shortner API from a WP7 app, I tried first on a Console Application by doing this:
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/urlshortener/v1/url");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{\"longUrl\":\"http://www.google.com/\"}";
Console.WriteLine(json);
streamWriter.Write(json);
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var responseText = streamReader.ReadToEnd();
Console.WriteLine(responseText);
}
Console.Read();
and it worked fine, and returned everything OK, but when I try to do it on a Windows Phone App like this:
private void button1_Click(object sender, RoutedEventArgs e)
{
testConnection();
}
private void testConnection()
{
if (!NetworkInterface.GetIsNetworkAvailable())
MessageBox.Show("There's no internet connection, please reconnect to the internet and try again");
else
{
var req = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/urlshortener/v1/url");
req.ContentType = "application/json";
req.Method = "POST";
req.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), req);
textBlock2.Text = "Done";
}
}
void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
// End the stream request operation
Stream postStream = webRequest.EndGetRequestStream(asynchronousResult);
// Create the post data
// Demo POST data:
string postData = "http://www.google.com";
byte[] byteArray = Encoding.Unicode.GetBytes(postData);
// Add the post data to the web request
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();
// Start the web request
webRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), webRequest);
}
void GetResponseCallback(IAsyncResult asynchronousResult)
{
try
{
HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response;
// End the get response operation
response = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult);
Stream streamResponse = response.GetResponseStream();
StreamReader streamReader = new StreamReader(streamResponse);
textBlock1.Text= streamReader.ReadToEnd();
streamResponse.Close();
streamReader.Close();
response.Close();
}
catch (WebException e)
{
}
}
The code always goes to the catch of the try method and gives "not found" error.
There is a known issue with SSL and not-trusted certificates. Connection to a web site with SSL that require ClientCertificates is not supported in the current Windows Phone 7 application model (Only BasicAuthentication is supported). You can read about the same problem here: http://forums.create.msdn.com/forums/p/65076/398730.aspx
I had a similar issue (which makes sense since I started out with this code as my example...) and when I changed the following code it started working to the point where I could progress forward again.
... from:
byte[] byteArray = Encoding.Unicode.GetBytes(postData);
... to (the equivalent of):
byte[] byteArray = Encoding.UTF8.GetBytes(postData);

POST Requests on WP7

I've been dying for about 6 hours trying to figure out how to make a regular POST request in WP7 , I tried the answers of similar questions posted here and on many other places, I also tried many different APIs POST request, they all lead to one certain problem,
The remote server returned an error: NotFound.
it seems like everytime there's something missing.
So, if you please someone show us how to properly get a POST request right in this WP7
I use this to post to facebook without any problem:
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUrl);
request.Method = "POST";
request.BeginGetResponse((e) =>
{
try
{
WebResponse response = request.EndGetResponse(e);
// Do Stuff
}
catch (WebException ex)
{
// Handle
}
catch (Exception ex)
{
// Handle
}
}, null);
I assume you would have tried this already so it may be something to do with the post data (which isn't in the above example as facebook uses the query string). Can you give us any more info?
EDIT: This is an (untested) example for writing post data:
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUrl);
request.Method = "POST";
request.BeginGetRequestStream((e) =>
{
using (Stream stream = request.EndGetRequestStream(e))
{
// Write data to the request stream
}
request.BeginGetResponse((callback) =>
{
try
{
WebResponse response = request.EndGetResponse(callback);
// Do Stuff
}
catch (WebException ex)
{
// Handle
}
catch (Exception ex)
{
// Handle
}
}, null);
}, null);
I use the following class for making POST requests with WP7:
public class PostMultiPartFormData
{
private Dictionary<string, object> Parameters;
private Encoding ENCODING = Encoding.UTF8;
private string BOUNDARY = "-----------------------------wp7postrequest";
public event EventHandler PostComplete;
public void Post(string postbackURL,
Dictionary<string, object> parameters)
{
Parameters = parameters;
Uri url = null;
url = new Uri(postbackURL);
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "POST";
request.ContentType = "multipart/form-data; boundary=" + BOUNDARY;
request.BeginGetRequestStream(new AsyncCallback(SendStatusUpdate), request);
}
private void SendStatusUpdate(IAsyncResult ar)
{
HttpWebRequest request = (HttpWebRequest)ar.AsyncState;
Stream stream = request.EndGetRequestStream(ar);
byte[] byteArray = GetMultipartFormData(Parameters, BOUNDARY);
stream.Write(byteArray, 0, byteArray.Length);
stream.Close();
stream.Dispose();
request.BeginGetResponse(new AsyncCallback(StatusUpdateCompleted), request);
}
private byte[] GetMultipartFormData(Dictionary<string, object> postParameters, string boundary)
{
Stream formDataStream = new System.IO.MemoryStream();
foreach (var param in postParameters)
{
if (param.Value is byte[])
{
byte[] fileData = param.Value as byte[];
string header = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{2}.jpg\";\r\nContent-Type: application/octet-stream\r\n\r\n", boundary, param.Key, param.Key);
formDataStream.Write(ENCODING.GetBytes(header), 0, header.Length);
formDataStream.Write(fileData, 0, fileData.Length);
}
else
{
string postData = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n{2}\r\n", boundary, param.Key, param.Value);
byte[] b = ENCODING.GetBytes(postData);
foreach (var item in b)
{
formDataStream.WriteByte(item);
}
}
}
string footer = "\r\n--" + boundary + "--\r\n";
byte[] footerbytes = ENCODING.GetBytes(footer);
formDataStream.Write(footerbytes, 0, footerbytes.Length);
formDataStream.Position = 0;
byte[] formData = new byte[formDataStream.Length];
formDataStream.Read(formData, 0, formData.Length);
formDataStream.Flush();
formDataStream.Close();
return formData;
}
private void StatusUpdateCompleted(IAsyncResult ar)
{
if (PostComplete != null)
{
PostComplete(ar, null);
}
}
}
Example:
PostMultiPartFormData postRequest = new PostMultiPartFormData();
postRequest.PostComplete += new EventHandler( (sender, e) =>
{
IAsyncResult ar = ((IAsyncResult)sender);
using (WebResponse resp = ((HttpWebRequest)ar.AsyncState).EndGetResponse(ar))
{
using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
{
string responseString = sr.ReadToEnd();
this.Dispatcher.BeginInvoke(() =>
{
textBlock.Text = responseString;
});
}
}
});
postRequest.Post("http://localhost:50624/SSLProxy.ashx",
new Dictionary<string, object>() { { "param1", "value1" } });
This should work...
If it doesn't let me know! :-)
For easier access to advanced http features check out these http classes:
http://mytoolkit.codeplex.com/wikipage?title=Http
It encapsulates GET, POST, FILES (using path or Stream objects) and GZIP (not directly supported by WP7) requests.
To add post data just call BeginGetRequestStream method (also, BeginGetResponse move to GetRequestStreamCallback)
request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);
void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
// End the stream request operation
Stream postStream = webRequest.EndGetRequestStream(asynchronousResult);
// Create the post data
string postData = "post data";
byte[] byteArray = Encoding.Unicode.GetBytes(postData);
// Add the post data to the web request
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();
// Start the web request
webRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), webRequest);
}
I recommend you to use the postclient. It is pretty simple. You just need to add reference to dll file into your project, and then write something like:
public void authorize(string login, string password)
{
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("command", "login");
parameters.Add("username", login);
parameters.Add("password", password);
PostClient proxy = new PostClient(parameters);
proxy.DownloadStringCompleted += (sender, e) =>
{
if (e.Error == null)
{
MessageBox.Show(e.Result);
}
};
proxy.DownloadStringAsync(new Uri("http://address.com/service", UriKind.Absolute));
}

HttpWebRequest on Wp7 AsyncWaitHandle has System.NotSupportedException

I'm having trouble with a basic HttpWebRequest in a WP7 application. As soon as I call BeginGetRequestStream, it starts to go wrong. The callback method then receives an IAsyncResult with no AsyncWaitHandle.
AsyncWaitHandle = 'ar.AsyncWaitHandle' threw an exception of type 'System.NotSupportedException'
I've created a sample below that produces the error. To create this I have just taken the sample pivot application and attempted to post some data to a server in the constructor of the MainViewModel.
public MainViewModel()
{
this.Items = new ObservableCollection<ItemViewModel>();
HttpWebRequest webRequest = WebRequest.CreateHttp("http://www.google.co.uk");
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.BeginGetRequestStream(BeginGetRequestStreamCallBack, webRequest);
}
private void BeginGetRequestStreamCallBack(IAsyncResult ar)
{
HttpWebRequest webRequest = (HttpWebRequest) ar.AsyncState;
Stream requestStream = webRequest.EndGetRequestStream(ar);
using(StreamWriter sw = new StreamWriter(requestStream))
{
sw.Write("{ test: \"test\" }");
}
webRequest.BeginGetResponse(BeginGetResponseCallback, webRequest);
}
private void BeginGetResponseCallback(IAsyncResult ar)
{
HttpWebRequest webRequest = (HttpWebRequest)ar.AsyncState;
WebResponse webResponse = webRequest.EndGetResponse(ar);
Stream responseStream = webResponse.GetResponseStream();
string whatCameBack = new StreamReader(responseStream).ReadToEnd();
}
Any help would as always, be greatly appreciated.
I've looked into this a little deeper, I just can't seem to POST data using BeginGetRequestStream.
public MainViewModel()
{
this.Items = new ObservableCollection<ItemViewModel>();
HttpWebRequest webRequest = WebRequest.CreateHttp("http://www.google.co.uk");
//webRequest.Method = "POST";
//webRequest.ContentType = "application/x-www-form-urlencoded";
//webRequest.BeginGetRequestStream(BeginGetRequestStreamCallBack, webRequest);
webRequest.BeginGetResponse(BeginGetResponseCallback, webRequest);
}
private void BeginGetRequestStreamCallBack(IAsyncResult ar)
{
HttpWebRequest webRequest = (HttpWebRequest) ar.AsyncState;
Stream requestStream = webRequest.EndGetRequestStream(ar);
using(StreamWriter sw = new StreamWriter(requestStream))
{
sw.Write("{ test: \"test\" }");
}
webRequest.BeginGetResponse(BeginGetResponseCallback, webRequest);
}
private void BeginGetResponseCallback(IAsyncResult ar)
{
HttpWebRequest webRequest = (HttpWebRequest)ar.AsyncState;
WebResponse webResponse = webRequest.EndGetResponse(ar);
Stream responseStream = webResponse.GetResponseStream();
string whatCameBack = new StreamReader(responseStream).ReadToEnd();
}
This works okay, but it is not what I want.
Any ideas?
I was being a naughty boy and not Closing the stream correctly.
using(Stream requestStream = webRequest.EndGetRequestStream(ar))
{}
All is well.

HttpWebRequest.EndGetResponse throws a NotSupportedException in Windows Phone 7

in a Silverlight-Windows Phone 7-project I am creating an HttpWebRequest, get the RequestStream, write something into the Stream and try to get the response, but I always get a NotSupportedException:
"System.Net.Browser.OHWRAsyncResult.AsyncWaitHandle threw an exception of type 'System.NotSupportedException'
My production code is far more complicated, but I can narrow it down to this small piece of code:
public class HttpUploadHelper
{
private HttpWebRequest request;
private RequestState state = new RequestState();
public HttpUploadHelper(string url)
{
this.request = WebRequest.Create(url) as HttpWebRequest;
state.Request = request;
}
public void Execute()
{
request.Method = "POST";
this.request.BeginGetRequestStream(
new AsyncCallback(BeginRequest), state);
}
private void BeginRequest(IAsyncResult ar)
{
Stream stream = state.Request.EndGetRequestStream(ar);
state.Request.BeginGetResponse(
new AsyncCallback(BeginResponse), state);
}
private void BeginResponse(IAsyncResult ar)
{
// BOOM: NotSupportedException was unhandled;
// {System.Net.Browser.OHWRAsyncResult}
// AsyncWaitHandle = 'ar.AsyncWaitHandle' threw an
// exception of type 'System.NotSupportedException'
HttpWebResponse response = state.Request.EndGetResponse(ar) as HttpWebResponse;
Debug.WriteLine(response.StatusCode);
}
}
public class RequestState
{
public WebRequest Request;
}
}
Does anybody know what is wrong with this code?
The NotSupportedException can be thrown when the request stream isn't closed before the call to EndGetResponse. The WebRequest stream is still open and sending data to the server when you're attempting to get the response. Since stream implements the IDisposable interface, a simple solution is to wrap your code using the request stream in a using block:
private void BeginRequest(IAsyncResult ar)
{
using (Stream stream = request.EndGetRequestStream(ar))
{
//write to stream in here.
}
state.Request.BeginGetResponse(
new AsyncCallback(BeginResponse), state);
}
The using block will ensure that the stream is closed before you attempt to get the response from the web server.
The issue is with how you're dealing with the accessing the original requests in the callback from BeginGetResponse.
Rather than holding a reference ot the state, get a reference back to the original request with:
var request = (HttpWebRequest)asynchronousResult.AsyncState;
Have a look at this very basic (but working) example of implementing logging in by posting email and password credentials to a website.
public static string Email;
public static string Password;
private void LoginClick(object sender, RoutedEventArgs e)
{
Email = enteredEmailAddress.Text.Trim().ToLower();
Password = enteredPassword.Password;
var request = (HttpWebRequest)WebRequest.Create(App.Config.ServerUris.Login);
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
request.BeginGetRequestStream(ReadCallback, request);
}
private void ReadCallback(IAsyncResult asynchronousResult)
{
var request = (HttpWebRequest)asynchronousResult.AsyncState;
using (var postStream = request.EndGetRequestStream(asynchronousResult))
{
using (var memStream = new MemoryStream())
{
var content = string.Format("Password={0}&Email={1}",
HttpUtility.UrlEncode(Password),
HttpUtility.UrlEncode(Email));
var bytes = System.Text.Encoding.UTF8.GetBytes(content);
memStream.Write(bytes, 0, bytes.Length);
memStream.Position = 0;
var tempBuffer = new byte[memStream.Length];
memStream.Read(tempBuffer, 0, tempBuffer.Length);
postStream.Write(tempBuffer, 0, tempBuffer.Length);
}
}
request.BeginGetResponse(ResponseCallback, request);
}
private void ResponseCallback(IAsyncResult asynchronousResult)
{
var request = (HttpWebRequest)asynchronousResult.AsyncState;
using (var resp = (HttpWebResponse)request.EndGetResponse(asynchronousResult))
{
using (var streamResponse = resp.GetResponseStream())
{
using (var streamRead = new StreamReader(streamResponse))
{
string responseString = streamRead.ReadToEnd();
// do something with responseString to check if login was successful
}
}
}
}
NotSupportedException can also be thrown when string url is too long. I had messed up and attached post data to url instead of post body. When the data was short, it worked just fine, but once it grew too large - EndGetResponse crashed.
Change this:
state.Request.BeginGetResponse(
new AsyncCallback(BeginResponse), state);
To this:
state.Request.BeginGetResponse(BeginResponse, state);

Resources