Reputation: 23
I want to read Json string from URL, but when I using Chrome web page will show this string:
And when I using C# WebBrowser it ask me download file to read I try to use WebClient DownloadString but this site use cookie from login page and it show arlert "Request Invaild":
Please help me, I'm sorry if my English is not good
Update: Here is my code:
wbTest.Navigate("http://authen.dzogame.com:8080/LauncherLogin.aspx?gid=200");
wbTest.Document.GetElementById("tbxUserName").SetAttribute("value", "7honda");
wbTest.Document.GetElementById("tbxPassword").SetAttribute("value", "111111");
wbTest.Document.GetElementById("btnLogin").InvokeMember("click");
Update 2: I try to get/set cookie, but it's not working.
wb.Navigate("http://authen.dzogame.com:8080/LauncherLogin.aspx?gid=200");
Wait();
id = txtID.Text;
pass = txtPassword.Text;
wb.Document.GetElementById("tbxUserName").SetAttribute("value", id);
wb.Document.GetElementById("tbxPassword").SetAttribute("value", pass);
wb.Document.GetElementById("btnLogin").InvokeMember("Click");
Wait();
string cookie = wb.Document.Cookie;
WebClient wc = new WebClient();
wc.Headers.Add(HttpRequestHeader.Cookie, cookie);
string token = wc.DownloadString("http://authen.dzogame.com:8080/ReturnLogin.ashx");
Upvotes: 2
Views: 2200
Reputation: 814
You trying to send the query with cookies from your browser control but the property cookie doesn't contain httpOnly cookie => so you you don't have the session cookie.
Getting the httpOnly is a little bit triky.
Add this code to your class so you can get the httpOnly cookie.
[DllImport("wininet.dll", SetLastError = true)]
public static extern bool InternetGetCookieEx(
string url,
string cookieName,
StringBuilder cookieData,
ref int size,
Int32 dwFlags,
IntPtr lpReserved);
private const Int32 InternetCookieHttponly = 0x2000;
/// <summary>
/// Gets the URI cookie container.
/// </summary>
/// <param name="uri">The URI.</param>
/// <returns></returns>
public static string GetUriCookieContainer(Uri uri)
{
// Determine the size of the cookie
int datasize = 8192 * 16;
StringBuilder cookieData = new StringBuilder(datasize);
if (!InternetGetCookieEx(uri.ToString(), null, cookieData, ref datasize, InternetCookieHttponly, IntPtr.Zero))
{
if (datasize < 0)
return null;
// Allocate stringbuilder large enough to hold the cookie
cookieData = new StringBuilder(datasize);
if (!InternetGetCookieEx(
uri.ToString(),
null, cookieData,
ref datasize,
InternetCookieHttponly,
IntPtr.Zero))
return null;
}
return cookieData.ToString();
}
You can get the json like with the code below
private void func()
{
// your code for login here ....
var urlJson = new Uri("http://authen.dzogame.com:8080/LauncherLogin.aspx?gid=200");
var cookie = GetUriCookieContainer(urlJson);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://authen.dzogame.com:8080/ReturnLogin.ashx");
request.Headers.Add(HttpRequestHeader.Cookie, cookie);
request.Accept = "*/*";
string jsonResponse = null;
using (WebResponse response = request.GetResponse())
{
using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
{
jsonResponse = streamReader.ReadToEnd();
}
}
}
Upvotes: 2
Reputation: 87
Take a look at this open source JSON Framework : http://www.newtonsoft.com/json
dynamic responseObject = JsonConvert.DeserializeObject("{\"status\":\"1\"...");
Upvotes: 1