I want to download string by calling api in which i have to pass some parameters
which contains space like :
String myUrl = AppConstants.BASE_URL + AppConstants.getFileteredList + Name + "," + LastName;
WebClient wc = new WebClient();
Uri uri = new Uri(myUrl,UriKind.Absolute);
wc.DownloadStringAsync(uri);
wc.DownloadStringCompleted += wc_DownloadStringCompleted;
it looks like this
http://demo.com/Api/getFileteredList?data=abc%20xyz,abc%20xyz
but in wc_DownloadStringCompleted , e.result throws and exception like
The remote server returned an error: NotFound.
i have tried "Uri.EscapeUriString" and also tried to implement using "HttpWebRequest" but getting same error.
Please help me out this.
Thank You in advance.
I believe its just a Malformed URL. If you peek myUrl you would notice a missing ? or something probably.
Can you check and port more details in your question.
Related
Requesting ICloud API and login by using below URL
string url = "https://setup.icloud.com/setup/ws/1/login?clientBuildNumber=1P24&clientId=" + RANDOM_GUID;
Getting server url and dsid from above url response.
Further requesting for Contact list by using below code
var localUrl2 = "https://p66-contactsws.icloud.com/co/startup?clientBuildNumber=1P24&clientId=" + RANDOM_GUID + "&clientVersion=2.1&dsid=" + dsid + "&locale=en-EN&order=last%2Cfirst";
var webRequest2 = (HttpWebRequest)WebRequest.Create(localUrl2);
webRequest2.Method = "GET";
webRequest2.Headers.Set("Origin", "https://www.icloud.com");
// get the X-APPLE-WEBAUTH-TOKEN and X-APPLE-WEBAUTH-USER from webResponse.Headers.SetCookie
webRequest2.Headers.Set("X-APPLE-WEBAUTH-TOKEN", XXXXXXX);
webRequest2.Headers.Set("X-APPLE-WEBAUTH-USER", XXXXXXXX);
WebResponse webResponse2 = webRequest2.GetResponse();
Still getting exception
The remote server returned an error: (421) Misdirected Request.
Please let me know what is wrong in above code.
As mentioned in the comments, Apple has taken the end point you are trying to reach offline hence the 421 error. You can look into another way to accomplish this here.
This is wrecking my brain for a while now. The code works fine when I post a smaller XML string. As soon as I add more XML nodes and post, I get the infamous '404 Error'
I'm posting XML data as a string to a Generic Handler in C#.
string strXML = "Large XML Content Here";
WebClient client = new WebClient();
string xmlResult = "";
try
{
xmlResult = client.DownloadString(_workContext.AccountingWebServiceLink
+ "?action=updateprimary&xml="
+ System.Web.HttpUtility.UrlEncode(strXML));
}
catch(Exception e)
{
System.IO.File.AppendAllText(Server.MapPath("~/addressXMLReturnError.txt"),
"Error: " + e.Message + " returnValue = " + xmlResult);
}
I think it might have something to do with the server not accepting large strings?
Finally got it working using suggestions from comments above.
So posting the XML data is the best route for me.
Using code from this SO post > HTTP post XML data in C#
To receive the data on the other side, check out this link > https://code.msdn.microsoft.com/Enviar-datos-por-el-metodo-2e580ace
I have, in my silverlight application, a call to a aspx page, to create and register a txt file on a directory.
Uri ub = (new Uri(HtmlPage.Document.DocumentUri, "GenereInfos.aspx?&connexion=" + connexion + ";&id=" + this.Id));
if (HtmlPage.IsPopupWindowAllowed)
{
HtmlPopupWindowOptions opt = new HtmlPopupWindowOptions();
HtmlPage.PopupWindow(ub, "file", opt);
}
else
{
HtmlPage.Window.Navigate(ub);
}
I have to go trough my aspx page to generate my txt file, because silverlight don't allow it.
The problem here is, a popup will appear, or the page will load the new uri.
What I want is call the code inside the asp only(which works perfectly), without loading the uri.
Is there a way to do this?
Edit : After DGibbs answer, there is another question now :
WShy can't I use GetResponse() in there?
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(new Uri
(HtmlPage.Document.DocumentUri, "GenereInfos.aspx?&connexion=" + connexion + ";&idPocedure=" + itmProcedure.IdProcedure));
string response = new System.IO.StreamReader(req.GetResponse().GetResponseStream()).ReadToEnd();
Here a little answer : Silverlight is asynchrnous, so, we can't call GetResponse who is synchronous.
So, the best way to call my aspx page, is to use WebClient found here
You could just use WebRequest:
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(new Uri
(HtmlPage.Document.DocumentUri, "GenereInfos.aspx?&connexion=" + connexion + ";&id=" + this.Id));
string response = new System.IO.StreamReader(req.GetResponse()
.GetResponseStream()).ReadToEnd();
I want to pass URL and my code is:
MyUrl = "http://www.abc.co.in/Download.aspx?period=" + Server.UrlEncode
(DateTime.Now.ToString("dd-MMM-yyyy")) + "&ProductName="
+ Server.UrlEncode(productName) + "";
mail.Body += "Demo Download";
But still I'm getting output like:
http://www.abc.co.in/Download.aspx?period=12-Apr-2013&ProductName=Otja
So what is wrong with my code and how to decode it on download.aspx?
Use HttpUtility.UrlEncode from System.Web namespace.
HttpUtility.UrlEncode Method : MSDN Link
You have given a specific format for datetime which (dd-MMM-yy), there is nothing in this string which should be encoded by UrlEncode function.
What i am trying to say can be explained by trying the code below
Response.Redirect("~/Test.aspx" + Server.UrlEncode(DateTime.Now.ToString("dd:MMM:yyyy")));
I'm using WebClient to do a POST to a server like so:
string URI = "http://mydomain.com/foo";
string myParameters =
"&token=1234" +
"&text=" + HttpUtility.UrlEncode(someVariable);
using (WebClient wc = new WebClient())
{
try
{
wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
string HtmlResult = wc.UploadString(URI, myParameters);
}
catch (WebException e)
{
return e.Message;
}
}
I sometimes can get exceptions thrown due to HTTP 403 (forbidden). In those cases, I want to know the exact reason. Digging into the service I'm calling into, it is documneted that it will optionally return an errorDetail field like so:
"code": 403,
"errorType": "not_authorized",
"errorDetail": "Can reply on a checkin at most once."
However, when I'm stepping through the code in Visual Studio, I don't see how I can get the errorDetail field. It doesn't apepar to be part of the WebException.
Is there a way to get it so I could display is something like:
return e.Message + " -- " + e.errorDetail
?
errorDetail is not part of the HTTP specification so WebClient does not know about it. For the same reason you will not find it anywhere in the BCL.
What you probably want is the response body so you can parse it (maybe as JSON? Your data looks like it).
You can get the response object from WebException.Response. Read it just like you normally would read a response.
Be aware that this property might be null if there is no response (due to network error, for example).